]> git.lizzy.rs Git - rust.git/commitdiff
Improve specialization test
authorJonas Schievink <jonasschievink@gmail.com>
Sun, 15 Sep 2019 20:02:44 +0000 (22:02 +0200)
committerJonas Schievink <jonasschievink@gmail.com>
Fri, 21 Feb 2020 18:41:22 +0000 (19:41 +0100)
src/test/ui/associated-types/defaults-specialization.rs
src/test/ui/associated-types/defaults-specialization.stderr

index e3a5db0960cf0ff8ed8d01f956127cae7dfeba16..833981fc8e3c89a2570c55006586394f94a18583 100644 (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;
+}
index 2b2adfdb7c9650e8df9885f2500893939ddb11b0..bd0fd7166f6681ee565b69aa243210ed4b42cd89 100644 (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`.