1 //! Tests the interaction of associated type defaults and specialization.
5 #![feature(associated_type_defaults, specialization)]
10 fn make() -> Self::Ty {
12 //~^ error: mismatched types
17 // In a `default impl`, assoc. types are defaulted as well,
18 // so their values can't be assumed.
19 default impl<T> Tr for A<T> {
21 //~^ ERROR method `make` has an incompatible type for trait
25 // ...same, but in the method body
26 default impl<T> Tr for A2<T> {
27 fn make() -> Self::Ty { 0u8 }
28 //~^ ERROR mismatched types
32 // Explicitly defaulting the type does the same.
34 default type Ty = bool;
36 fn make() -> bool { true }
37 //~^ ERROR method `make` has an incompatible type for trait
41 // ...same, but in the method body
42 impl<T> Tr for B2<T> {
43 default type Ty = bool;
45 fn make() -> Self::Ty { true }
46 //~^ ERROR mismatched types
50 // Only the method is defaulted, so this is fine.
54 default fn make() -> bool { true }
57 // Defaulted method *can* assume the type, if the default is kept.
60 default fn make() -> u8 { 0 }
64 fn make() -> u8 { 255 }
69 default fn make() -> Self::Ty { panic!(); }
72 // This impl specializes and sets `Ty`, it can rely on `Ty=String`.
76 fn make() -> String { String::new() }
80 // Test that we can assume the right set of assoc. types from outside the impl
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;
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
92 let _: <C<()> as Tr>::Ty = true;
94 let _: <D<()> as Tr>::Ty = 0u8;
95 let _: <D<bool> as Tr>::Ty = 0u8;