Improve specialization test

This commit is contained in:
Jonas Schievink 2019-09-15 22:02:44 +02:00
parent fbcd136b65
commit c8da9ee50a
2 changed files with 88 additions and 9 deletions

View File

@ -7,7 +7,10 @@
trait Tr {
type Ty = u8;
fn make() -> Self::Ty;
fn make() -> Self::Ty {
0u8
//~^ error: mismatched types
}
}
struct A<T>(T);
@ -61,4 +64,33 @@ impl Tr for D<bool> {
fn make() -> u8 { 255 }
}
fn main() {}
struct E<T>(T);
impl<T> Tr for E<T> {
default fn make() -> Self::Ty { panic!(); }
}
// This impl specializes and sets `Ty`, it can rely on `Ty=String`.
impl Tr for E<bool> {
type Ty = String;
fn make() -> String { String::new() }
}
fn main() {
// Test that we can assume the right set of assoc. types from outside the impl
// This is a `default impl`, which does *not* mean that `A`/`A2` actually implement the trait.
// cf. https://github.com/rust-lang/rust/issues/48515
//let _: <A<()> as Tr>::Ty = 0u8;
//let _: <A2<()> as Tr>::Ty = 0u8;
let _: <B<()> as Tr>::Ty = 0u8; //~ error: mismatched types
let _: <B<()> as Tr>::Ty = true; //~ error: mismatched types
let _: <B2<()> as Tr>::Ty = 0u8; //~ error: mismatched types
let _: <B2<()> as Tr>::Ty = true; //~ error: mismatched types
let _: <C<()> as Tr>::Ty = true;
let _: <D<()> as Tr>::Ty = 0u8;
let _: <D<bool> as Tr>::Ty = 0u8;
}

View File

@ -1,7 +1,7 @@
error[E0053]: method `make` has an incompatible type for trait
--> $DIR/defaults-specialization.rs:17:18
--> $DIR/defaults-specialization.rs:20:18
|
LL | fn make() -> Self::Ty;
LL | fn make() -> Self::Ty {
| -------- type in trait
...
LL | fn make() -> u8 { 0 }
@ -11,9 +11,9 @@ LL | fn make() -> u8 { 0 }
found type `fn() -> u8`
error[E0053]: method `make` has an incompatible type for trait
--> $DIR/defaults-specialization.rs:33:18
--> $DIR/defaults-specialization.rs:36:18
|
LL | fn make() -> Self::Ty;
LL | fn make() -> Self::Ty {
| -------- type in trait
...
LL | fn make() -> bool { true }
@ -23,7 +23,18 @@ LL | fn make() -> bool { true }
found type `fn() -> bool`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:24:29
--> $DIR/defaults-specialization.rs:11:9
|
LL | fn make() -> Self::Ty {
| -------- expected `<Self as Tr>::Ty` because of return type
LL | 0u8
| ^^^ expected associated type, found u8
|
= note: expected type `<Self as Tr>::Ty`
found type `u8`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:27:29
|
LL | fn make() -> Self::Ty { 0u8 }
| -------- ^^^ expected associated type, found u8
@ -34,7 +45,7 @@ LL | fn make() -> Self::Ty { 0u8 }
found type `u8`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:42:29
--> $DIR/defaults-specialization.rs:45:29
|
LL | fn make() -> Self::Ty { true }
| -------- ^^^^ expected associated type, found bool
@ -44,7 +55,43 @@ LL | fn make() -> Self::Ty { true }
= note: expected type `<B2<T> as Tr>::Ty`
found type `bool`
error: aborting due to 4 previous errors
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:87:32
|
LL | let _: <B<()> as Tr>::Ty = 0u8;
| ^^^ expected associated type, found u8
|
= note: expected type `<B<()> as Tr>::Ty`
found type `u8`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:88:32
|
LL | let _: <B<()> as Tr>::Ty = true;
| ^^^^ expected associated type, found bool
|
= note: expected type `<B<()> as Tr>::Ty`
found type `bool`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:89:33
|
LL | let _: <B2<()> as Tr>::Ty = 0u8;
| ^^^ expected associated type, found u8
|
= note: expected type `<B2<()> as Tr>::Ty`
found type `u8`
error[E0308]: mismatched types
--> $DIR/defaults-specialization.rs:90:33
|
LL | let _: <B2<()> as Tr>::Ty = true;
| ^^^^ expected associated type, found bool
|
= note: expected type `<B2<()> as Tr>::Ty`
found type `bool`
error: aborting due to 9 previous errors
Some errors have detailed explanations: E0053, E0308.
For more information about an error, try `rustc --explain E0053`.