Skip to content

Commit c8da9ee

Browse files
Improve specialization test
1 parent fbcd136 commit c8da9ee

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

src/test/ui/associated-types/defaults-specialization.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
trait Tr {
88
type Ty = u8;
99

10-
fn make() -> Self::Ty;
10+
fn make() -> Self::Ty {
11+
0u8
12+
//~^ error: mismatched types
13+
}
1114
}
1215

1316
struct A<T>(T);
@@ -61,4 +64,33 @@ impl Tr for D<bool> {
6164
fn make() -> u8 { 255 }
6265
}
6366

64-
fn main() {}
67+
struct E<T>(T);
68+
impl<T> Tr for E<T> {
69+
default fn make() -> Self::Ty { panic!(); }
70+
}
71+
72+
// This impl specializes and sets `Ty`, it can rely on `Ty=String`.
73+
impl Tr for E<bool> {
74+
type Ty = String;
75+
76+
fn make() -> String { String::new() }
77+
}
78+
79+
fn main() {
80+
// Test that we can assume the right set of assoc. types from outside the impl
81+
82+
// This is a `default impl`, which does *not* mean that `A`/`A2` actually implement the trait.
83+
// cf. https://github.com/rust-lang/rust/issues/48515
84+
//let _: <A<()> as Tr>::Ty = 0u8;
85+
//let _: <A2<()> as Tr>::Ty = 0u8;
86+
87+
let _: <B<()> as Tr>::Ty = 0u8; //~ error: mismatched types
88+
let _: <B<()> as Tr>::Ty = true; //~ error: mismatched types
89+
let _: <B2<()> as Tr>::Ty = 0u8; //~ error: mismatched types
90+
let _: <B2<()> as Tr>::Ty = true; //~ error: mismatched types
91+
92+
let _: <C<()> as Tr>::Ty = true;
93+
94+
let _: <D<()> as Tr>::Ty = 0u8;
95+
let _: <D<bool> as Tr>::Ty = 0u8;
96+
}

src/test/ui/associated-types/defaults-specialization.stderr

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0053]: method `make` has an incompatible type for trait
2-
--> $DIR/defaults-specialization.rs:17:18
2+
--> $DIR/defaults-specialization.rs:20:18
33
|
4-
LL | fn make() -> Self::Ty;
4+
LL | fn make() -> Self::Ty {
55
| -------- type in trait
66
...
77
LL | fn make() -> u8 { 0 }
@@ -11,9 +11,9 @@ LL | fn make() -> u8 { 0 }
1111
found type `fn() -> u8`
1212

1313
error[E0053]: method `make` has an incompatible type for trait
14-
--> $DIR/defaults-specialization.rs:33:18
14+
--> $DIR/defaults-specialization.rs:36:18
1515
|
16-
LL | fn make() -> Self::Ty;
16+
LL | fn make() -> Self::Ty {
1717
| -------- type in trait
1818
...
1919
LL | fn make() -> bool { true }
@@ -23,7 +23,18 @@ LL | fn make() -> bool { true }
2323
found type `fn() -> bool`
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/defaults-specialization.rs:24:29
26+
--> $DIR/defaults-specialization.rs:11:9
27+
|
28+
LL | fn make() -> Self::Ty {
29+
| -------- expected `<Self as Tr>::Ty` because of return type
30+
LL | 0u8
31+
| ^^^ expected associated type, found u8
32+
|
33+
= note: expected type `<Self as Tr>::Ty`
34+
found type `u8`
35+
36+
error[E0308]: mismatched types
37+
--> $DIR/defaults-specialization.rs:27:29
2738
|
2839
LL | fn make() -> Self::Ty { 0u8 }
2940
| -------- ^^^ expected associated type, found u8
@@ -34,7 +45,7 @@ LL | fn make() -> Self::Ty { 0u8 }
3445
found type `u8`
3546

3647
error[E0308]: mismatched types
37-
--> $DIR/defaults-specialization.rs:42:29
48+
--> $DIR/defaults-specialization.rs:45:29
3849
|
3950
LL | fn make() -> Self::Ty { true }
4051
| -------- ^^^^ expected associated type, found bool
@@ -44,7 +55,43 @@ LL | fn make() -> Self::Ty { true }
4455
= note: expected type `<B2<T> as Tr>::Ty`
4556
found type `bool`
4657

47-
error: aborting due to 4 previous errors
58+
error[E0308]: mismatched types
59+
--> $DIR/defaults-specialization.rs:87:32
60+
|
61+
LL | let _: <B<()> as Tr>::Ty = 0u8;
62+
| ^^^ expected associated type, found u8
63+
|
64+
= note: expected type `<B<()> as Tr>::Ty`
65+
found type `u8`
66+
67+
error[E0308]: mismatched types
68+
--> $DIR/defaults-specialization.rs:88:32
69+
|
70+
LL | let _: <B<()> as Tr>::Ty = true;
71+
| ^^^^ expected associated type, found bool
72+
|
73+
= note: expected type `<B<()> as Tr>::Ty`
74+
found type `bool`
75+
76+
error[E0308]: mismatched types
77+
--> $DIR/defaults-specialization.rs:89:33
78+
|
79+
LL | let _: <B2<()> as Tr>::Ty = 0u8;
80+
| ^^^ expected associated type, found u8
81+
|
82+
= note: expected type `<B2<()> as Tr>::Ty`
83+
found type `u8`
84+
85+
error[E0308]: mismatched types
86+
--> $DIR/defaults-specialization.rs:90:33
87+
|
88+
LL | let _: <B2<()> as Tr>::Ty = true;
89+
| ^^^^ expected associated type, found bool
90+
|
91+
= note: expected type `<B2<()> as Tr>::Ty`
92+
found type `bool`
93+
94+
error: aborting due to 9 previous errors
4895

4996
Some errors have detailed explanations: E0053, E0308.
5097
For more information about an error, try `rustc --explain E0053`.

0 commit comments

Comments
 (0)