Skip to content

Commit 1e3c020

Browse files
Test interactions with specialization
1 parent de447eb commit 1e3c020

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// compile-fail
2+
3+
#![feature(associated_type_defaults, specialization)]
4+
5+
trait Tr {
6+
type Ty = u8;
7+
8+
fn make() -> Self::Ty;
9+
}
10+
11+
struct A<T>(T);
12+
// In a `default impl`, assoc. types are defaulted as well,
13+
// so their values can't be assumed.
14+
default impl<T> Tr for A<T> {
15+
fn make() -> u8 { 0 }
16+
//~^ ERROR method `make` has an incompatible type for trait
17+
}
18+
19+
struct B<T>(T);
20+
// Explicitly defaulting the type does the same.
21+
impl<T> Tr for B<T> {
22+
default type Ty = bool;
23+
24+
fn make() -> bool { true }
25+
//~^ ERROR method `make` has an incompatible type for trait
26+
}
27+
28+
struct C<T>(T);
29+
// Only the method is defaulted, so this is fine.
30+
impl<T> Tr for C<T> {
31+
type Ty = bool;
32+
33+
default fn make() -> bool { true }
34+
}
35+
36+
// Defaulted method *can* assume the type, if the default is kept.
37+
struct D<T>(T);
38+
impl<T> Tr for D<T> {
39+
default fn make() -> u8 { 0 }
40+
}
41+
42+
impl Tr for D<bool> {
43+
fn make() -> u8 { 255 }
44+
}
45+
46+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0053]: method `make` has an incompatible type for trait
2+
--> $DIR/defaults-specialization.rs:15:18
3+
|
4+
LL | fn make() -> Self::Ty;
5+
| -------- type in trait
6+
...
7+
LL | fn make() -> u8 { 0 }
8+
| ^^ expected associated type, found u8
9+
|
10+
= note: expected type `fn() -> <A<T> as Tr>::Ty`
11+
found type `fn() -> u8`
12+
13+
error[E0053]: method `make` has an incompatible type for trait
14+
--> $DIR/defaults-specialization.rs:24:18
15+
|
16+
LL | fn make() -> Self::Ty;
17+
| -------- type in trait
18+
...
19+
LL | fn make() -> bool { true }
20+
| ^^^^ expected associated type, found bool
21+
|
22+
= note: expected type `fn() -> <B<T> as Tr>::Ty`
23+
found type `fn() -> bool`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0053`.

0 commit comments

Comments
 (0)