Skip to content

Commit 9155570

Browse files
committed
Add some tests
1 parent 4f8ebac commit 9155570

5 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Check that we cannot instantiate a hidden type in the body
2+
//! of an assoc fn or const unless mentioned in the signature.
3+
4+
#![feature(impl_trait_in_assoc_type)]
5+
6+
trait Trait: Sized {
7+
type Assoc;
8+
fn foo();
9+
fn bar() -> Self::Assoc;
10+
}
11+
12+
impl Trait for () {
13+
type Assoc = impl std::fmt::Debug;
14+
fn foo() {
15+
let x: Self::Assoc = 42; //~ ERROR: mismatched types
16+
}
17+
fn bar() -> Self::Assoc {
18+
""
19+
}
20+
}
21+
22+
trait Trait2: Sized {
23+
type Assoc;
24+
const FOO: ();
25+
fn bar() -> Self::Assoc;
26+
}
27+
28+
impl Trait2 for () {
29+
type Assoc = impl std::fmt::Debug;
30+
const FOO: () = {
31+
let x: Self::Assoc = 42; //~ ERROR: mismatched types
32+
};
33+
fn bar() -> Self::Assoc {
34+
""
35+
}
36+
}
37+
38+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:15:30
3+
|
4+
LL | type Assoc = impl std::fmt::Debug;
5+
| -------------------- the expected opaque type
6+
LL | fn foo() {
7+
LL | let x: Self::Assoc = 42;
8+
| ----------- ^^ expected opaque type, found integer
9+
| |
10+
| expected due to this
11+
|
12+
= note: expected opaque type `<() as Trait>::Assoc`
13+
found type `{integer}`
14+
note: this item must have the opaque type in its signature in order to be able to register hidden types
15+
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:14:8
16+
|
17+
LL | fn foo() {
18+
| ^^^
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:31:30
22+
|
23+
LL | type Assoc = impl std::fmt::Debug;
24+
| -------------------- the expected opaque type
25+
LL | const FOO: () = {
26+
LL | let x: Self::Assoc = 42;
27+
| ----------- ^^ expected opaque type, found integer
28+
| |
29+
| expected due to this
30+
|
31+
= note: expected opaque type `<() as Trait2>::Assoc`
32+
found type `{integer}`
33+
note: this item must have the opaque type in its signature in order to be able to register hidden types
34+
--> $DIR/impl_trait_in_trait_defined_outside_trait.rs:30:11
35+
|
36+
LL | const FOO: () = {
37+
| ^^^
38+
39+
error: aborting due to 2 previous errors
40+
41+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Check that we cannot instantiate a hidden type from another assoc type.
2+
3+
#![feature(impl_trait_in_assoc_type)]
4+
5+
trait Trait: Sized {
6+
type Assoc;
7+
type Foo;
8+
fn foo() -> Self::Assoc;
9+
}
10+
11+
impl Trait for () {
12+
type Assoc = impl std::fmt::Debug;
13+
type Foo = [(); {
14+
let x: Self::Assoc = 42; //~ ERROR: mismatched types
15+
3
16+
}];
17+
fn foo() -> Self::Assoc {
18+
""
19+
}
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/impl_trait_in_trait_defined_outside_trait2.rs:14:30
3+
|
4+
LL | type Assoc = impl std::fmt::Debug;
5+
| -------------------- the expected opaque type
6+
LL | type Foo = [(); {
7+
LL | let x: Self::Assoc = 42;
8+
| ----------- ^^ expected opaque type, found integer
9+
| |
10+
| expected due to this
11+
|
12+
= note: expected opaque type `<() as Trait>::Assoc`
13+
found type `{integer}`
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Check that non-defining assoc items can use the opaque type
2+
//! opaquely.
3+
4+
// check-pass
5+
6+
#![feature(impl_trait_in_assoc_type)]
7+
8+
trait Trait: Sized {
9+
type Assoc;
10+
fn foo();
11+
fn bar() -> Self::Assoc;
12+
}
13+
14+
impl Trait for () {
15+
type Assoc = impl std::fmt::Debug;
16+
fn foo() {
17+
let x: Self::Assoc = Self::bar();
18+
}
19+
fn bar() -> Self::Assoc {
20+
""
21+
}
22+
}
23+
24+
trait Trait2: Sized {
25+
type Assoc;
26+
const FOO: ();
27+
const BAR: Self::Assoc;
28+
}
29+
30+
impl Trait2 for () {
31+
type Assoc = impl Copy;
32+
const FOO: () = {
33+
let x: Self::Assoc = Self::BAR;
34+
};
35+
const BAR: Self::Assoc = "";
36+
}
37+
38+
fn main() {}

0 commit comments

Comments
 (0)