From 1e3c02006372ca68d07d32493660583cc1c9e12c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 14 Jun 2019 21:26:42 +0200 Subject: [PATCH] Test interactions with specialization --- .../defaults-specialization.rs | 46 +++++++++++++++++++ .../defaults-specialization.stderr | 27 +++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/test/ui/associated-types/defaults-specialization.rs create mode 100644 src/test/ui/associated-types/defaults-specialization.stderr diff --git a/src/test/ui/associated-types/defaults-specialization.rs b/src/test/ui/associated-types/defaults-specialization.rs new file mode 100644 index 00000000000..c8aab8fb3ec --- /dev/null +++ b/src/test/ui/associated-types/defaults-specialization.rs @@ -0,0 +1,46 @@ +// compile-fail + +#![feature(associated_type_defaults, specialization)] + +trait Tr { + type Ty = u8; + + fn make() -> Self::Ty; +} + +struct A(T); +// In a `default impl`, assoc. types are defaulted as well, +// so their values can't be assumed. +default impl Tr for A { + fn make() -> u8 { 0 } + //~^ ERROR method `make` has an incompatible type for trait +} + +struct B(T); +// Explicitly defaulting the type does the same. +impl Tr for B { + default type Ty = bool; + + fn make() -> bool { true } + //~^ ERROR method `make` has an incompatible type for trait +} + +struct C(T); +// Only the method is defaulted, so this is fine. +impl Tr for C { + type Ty = bool; + + default fn make() -> bool { true } +} + +// Defaulted method *can* assume the type, if the default is kept. +struct D(T); +impl Tr for D { + default fn make() -> u8 { 0 } +} + +impl Tr for D { + fn make() -> u8 { 255 } +} + +fn main() {} diff --git a/src/test/ui/associated-types/defaults-specialization.stderr b/src/test/ui/associated-types/defaults-specialization.stderr new file mode 100644 index 00000000000..0e6711780f8 --- /dev/null +++ b/src/test/ui/associated-types/defaults-specialization.stderr @@ -0,0 +1,27 @@ +error[E0053]: method `make` has an incompatible type for trait + --> $DIR/defaults-specialization.rs:15:18 + | +LL | fn make() -> Self::Ty; + | -------- type in trait +... +LL | fn make() -> u8 { 0 } + | ^^ expected associated type, found u8 + | + = note: expected type `fn() -> as Tr>::Ty` + found type `fn() -> u8` + +error[E0053]: method `make` has an incompatible type for trait + --> $DIR/defaults-specialization.rs:24:18 + | +LL | fn make() -> Self::Ty; + | -------- type in trait +... +LL | fn make() -> bool { true } + | ^^^^ expected associated type, found bool + | + = note: expected type `fn() -> as Tr>::Ty` + found type `fn() -> bool` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0053`.