]> git.lizzy.rs Git - rust.git/blob - tests/ui/variance/variance-regions-direct.rs
Move /src/test to /tests
[rust.git] / tests / ui / variance / variance-regions-direct.rs
1 // Test that we correctly infer variance for region parameters in
2 // various self-contained types.
3
4 #![feature(rustc_attrs)]
5
6 // Regions that just appear in normal spots are contravariant:
7
8 #[rustc_variance]
9 struct Test2<'a, 'b, 'c> { //~ ERROR [-, -, -]
10     x: &'a isize,
11     y: &'b [isize],
12     c: &'c str
13 }
14
15 // Those same annotations in function arguments become covariant:
16
17 #[rustc_variance]
18 struct Test3<'a, 'b, 'c> { //~ ERROR [+, +, +]
19     x: extern "Rust" fn(&'a isize),
20     y: extern "Rust" fn(&'b [isize]),
21     c: extern "Rust" fn(&'c str),
22 }
23
24 // Mutability induces invariance:
25
26 #[rustc_variance]
27 struct Test4<'a, 'b:'a> { //~ ERROR [-, o]
28     x: &'a mut &'b isize,
29 }
30
31 // Mutability induces invariance, even when in a
32 // contravariant context:
33
34 #[rustc_variance]
35 struct Test5<'a, 'b:'a> { //~ ERROR [+, o]
36     x: extern "Rust" fn(&'a mut &'b isize),
37 }
38
39 // Invariance is a trap from which NO ONE CAN ESCAPE.
40 // In other words, even though the `&'b isize` occurs in
41 // an argument list (which is contravariant), that
42 // argument list occurs in an invariant context.
43
44 #[rustc_variance]
45 struct Test6<'a, 'b:'a> { //~ ERROR [-, o]
46     x: &'a mut extern "Rust" fn(&'b isize),
47 }
48
49 // No uses at all is bivariant:
50
51 #[rustc_variance]
52 struct Test7<'a> { //~ ERROR [*]
53     x: isize
54 }
55
56 // Try enums too.
57
58 #[rustc_variance]
59 enum Test8<'a, 'b, 'c:'b> { //~ ERROR [+, -, o]
60     Test8A(extern "Rust" fn(&'a isize)),
61     Test8B(&'b [isize]),
62     Test8C(&'b mut &'c str),
63 }
64
65 fn main() {}