1 // Test `ignored_generic_bounds` lint warning about bounds in type aliases.
2 
3 // check-pass
4 #![allow(dead_code)]
5 
6 use std::rc::Rc;
7 
8 type SVec<T: Send + Send> = Vec<T>;
9 //~^ WARN bounds on generic parameters are not enforced in type aliases [type_alias_bounds]
10 type S2Vec<T> where T: Send = Vec<T>;
11 //~^ WARN where clauses are not enforced in type aliases [type_alias_bounds]
12 type VVec<'b, 'a: 'b + 'b> = (&'b u32, Vec<&'a i32>);
13 //~^ WARN bounds on generic parameters are not enforced in type aliases [type_alias_bounds]
14 type WVec<'b, T: 'b + 'b> = (&'b u32, Vec<T>);
15 //~^ WARN bounds on generic parameters are not enforced in type aliases [type_alias_bounds]
16 type W2Vec<'b, T> where T: 'b, T: 'b = (&'b u32, Vec<T>);
17 //~^ WARN where clauses are not enforced in type aliases [type_alias_bounds]
18 
19 static STATIC: u32 = 0;
20 
foo<'a>(y: &'a i32)21 fn foo<'a>(y: &'a i32) {
22     // If any of the bounds above would matter, the code below would be rejected.
23     // This can be seen when replacing the type aliases above by newtype structs.
24     // (The type aliases have no unused parameters to make that a valid transformation.)
25     let mut x: SVec<_> = Vec::new();
26     x.push(Rc::new(42)); // is not send
27 
28     let mut x: S2Vec<_> = Vec::new();
29     x.push(Rc::new(42)); // is not `Send`
30 
31     let mut x: VVec<'static, 'a> = (&STATIC, Vec::new());
32     x.1.push(y); // `'a: 'static` does not hold
33 
34     let mut x: WVec<'static, &'a i32> = (&STATIC, Vec::new());
35     x.1.push(y); // `&'a i32: 'static` does not hold
36 
37     let mut x: W2Vec<'static, &'a i32> = (&STATIC, Vec::new());
38     x.1.push(y); // `&'a i32: 'static` does not hold
39 }
40 
41 // Bounds are not checked either; i.e., the definition is not necessarily well-formed.
42 struct Sendable<T: Send>(T);
43 type MySendable<T> = Sendable<T>; // no error here!
44 
45 // However, bounds *are* taken into account when accessing associated types
46 trait Bound { type Assoc; }
47 type T1<U: Bound> = U::Assoc; //~ WARN not enforced in type aliases
48 type T2<U> where U: Bound = U::Assoc;  //~ WARN not enforced in type aliases
49 
50 // This errors:
51 // `type T3<U> = U::Assoc;`
52 // Do this instead:
53 type T4<U> = <U as Bound>::Assoc;
54 
55 // Make sure the help about associatd types is not shown incorrectly
56 type T5<U: Bound> = <U as Bound>::Assoc;  //~ WARN not enforced in type aliases
57 type T6<U: Bound> = ::std::vec::Vec<U>;  //~ WARN not enforced in type aliases
58 
main()59 fn main() {}
60