1 //! Tests the interaction of associated type defaults and specialization.
3 #![feature(associated_type_defaults, specialization)]
4 //~^ WARN the feature `specialization` is incomplete
9 fn make() -> Self::Ty {
11 //~^ error: mismatched types
16 // In a `default impl`, assoc. types are defaulted as well,
17 // so their values can't be assumed.
18 default impl<T> Tr for A<T> {
20 //~^ ERROR method `make` has an incompatible type for trait
24 // ...same, but in the method body
25 default impl<T> Tr for A2<T> {
26 fn make() -> Self::Ty { 0u8 }
27 //~^ ERROR mismatched types
31 // Explicitly defaulting the type does the same.
33 default type Ty = bool;
35 fn make() -> bool { true }
36 //~^ ERROR method `make` has an incompatible type for trait
40 // ...same, but in the method body
41 impl<T> Tr for B2<T> {
42 default type Ty = bool;
44 fn make() -> Self::Ty { true }
45 //~^ ERROR mismatched types
49 // Only the method is defaulted, so this is fine.
53 default fn make() -> bool { true }
56 // Defaulted method *can* assume the type, if the default is kept.
59 default fn make() -> u8 { 0 }
63 fn make() -> u8 { 255 }
68 default type Ty = bool;
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;