1 //! Tests the interaction of associated type defaults and specialization.
3 #![feature(associated_type_defaults, specialization)]
8 fn make() -> Self::Ty {
10 //~^ error: mismatched types
15 // In a `default impl`, assoc. types are defaulted as well,
16 // so their values can't be assumed.
17 default impl<T> Tr for A<T> {
19 //~^ ERROR method `make` has an incompatible type for trait
23 // ...same, but in the method body
24 default impl<T> Tr for A2<T> {
25 fn make() -> Self::Ty { 0u8 }
26 //~^ ERROR mismatched types
30 // Explicitly defaulting the type does the same.
32 default type Ty = bool;
34 fn make() -> bool { true }
35 //~^ ERROR method `make` has an incompatible type for trait
39 // ...same, but in the method body
40 impl<T> Tr for B2<T> {
41 default type Ty = bool;
43 fn make() -> Self::Ty { true }
44 //~^ ERROR mismatched types
48 // Only the method is defaulted, so this is fine.
52 default fn make() -> bool { true }
55 // Defaulted method *can* assume the type, if the default is kept.
58 default fn make() -> u8 { 0 }
62 fn make() -> u8 { 255 }
67 default type Ty = bool;
68 default fn make() -> Self::Ty { panic!(); }
71 // This impl specializes and sets `Ty`, it can rely on `Ty=String`.
75 fn make() -> String { String::new() }
79 // Test that we can assume the right set of assoc. types from outside the impl
81 // This is a `default impl`, which does *not* mean that `A`/`A2` actually implement the trait.
82 // cf. https://github.com/rust-lang/rust/issues/48515
83 //let _: <A<()> as Tr>::Ty = 0u8;
84 //let _: <A2<()> as Tr>::Ty = 0u8;
86 let _: <B<()> as Tr>::Ty = 0u8; //~ error: mismatched types
87 let _: <B<()> as Tr>::Ty = true; //~ error: mismatched types
88 let _: <B2<()> as Tr>::Ty = 0u8; //~ error: mismatched types
89 let _: <B2<()> as Tr>::Ty = true; //~ error: mismatched types
91 let _: <C<()> as Tr>::Ty = true;
93 let _: <D<()> as Tr>::Ty = 0u8;
94 let _: <D<bool> as Tr>::Ty = 0u8;