]> git.lizzy.rs Git - rust.git/commitdiff
Test interactions with specialization
authorJonas Schievink <jonasschievink@gmail.com>
Fri, 14 Jun 2019 19:26:42 +0000 (21:26 +0200)
committerJonas Schievink <jonasschievink@gmail.com>
Fri, 21 Feb 2020 18:41:21 +0000 (19:41 +0100)
src/test/ui/associated-types/defaults-specialization.rs [new file with mode: 0644]
src/test/ui/associated-types/defaults-specialization.stderr [new file with mode: 0644]

diff --git a/src/test/ui/associated-types/defaults-specialization.rs b/src/test/ui/associated-types/defaults-specialization.rs
new file mode 100644 (file)
index 0000000..c8aab8f
--- /dev/null
@@ -0,0 +1,46 @@
+// compile-fail
+
+#![feature(associated_type_defaults, specialization)]
+
+trait Tr {
+    type Ty = u8;
+
+    fn make() -> Self::Ty;
+}
+
+struct A<T>(T);
+// In a `default impl`, assoc. types are defaulted as well,
+// so their values can't be assumed.
+default impl<T> Tr for A<T> {
+    fn make() -> u8 { 0 }
+    //~^ ERROR method `make` has an incompatible type for trait
+}
+
+struct B<T>(T);
+// Explicitly defaulting the type does the same.
+impl<T> Tr for B<T> {
+    default type Ty = bool;
+
+    fn make() -> bool { true }
+    //~^ ERROR method `make` has an incompatible type for trait
+}
+
+struct C<T>(T);
+// Only the method is defaulted, so this is fine.
+impl<T> Tr for C<T> {
+    type Ty = bool;
+
+    default fn make() -> bool { true }
+}
+
+// Defaulted method *can* assume the type, if the default is kept.
+struct D<T>(T);
+impl<T> Tr for D<T> {
+    default fn make() -> u8 { 0 }
+}
+
+impl Tr for D<bool> {
+    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 (file)
index 0000000..0e67117
--- /dev/null
@@ -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() -> <A<T> 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() -> <B<T> as Tr>::Ty`
+              found type `fn() -> bool`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0053`.