]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/variance-regions-direct.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / test / compile-fail / variance-regions-direct.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we correctly infer variance for region parameters in
12 // various self-contained types.
13
14 #![feature(rustc_attrs)]
15
16 // Regions that just appear in normal spots are contravariant:
17
18 #[rustc_variance]
19 struct Test2<'a, 'b, 'c> { //~ ERROR [-, -, -]
20     x: &'a isize,
21     y: &'b [isize],
22     c: &'c str
23 }
24
25 // Those same annotations in function arguments become covariant:
26
27 #[rustc_variance]
28 struct Test3<'a, 'b, 'c> { //~ ERROR [+, +, +]
29     x: extern "Rust" fn(&'a isize),
30     y: extern "Rust" fn(&'b [isize]),
31     c: extern "Rust" fn(&'c str),
32 }
33
34 // Mutability induces invariance:
35
36 #[rustc_variance]
37 struct Test4<'a, 'b:'a> { //~ ERROR [-, o]
38     x: &'a mut &'b isize,
39 }
40
41 // Mutability induces invariance, even when in a
42 // contravariant context:
43
44 #[rustc_variance]
45 struct Test5<'a, 'b:'a> { //~ ERROR [+, o]
46     x: extern "Rust" fn(&'a mut &'b isize),
47 }
48
49 // Invariance is a trap from which NO ONE CAN ESCAPE.
50 // In other words, even though the `&'b isize` occurs in
51 // an argument list (which is contravariant), that
52 // argument list occurs in an invariant context.
53
54 #[rustc_variance]
55 struct Test6<'a, 'b:'a> { //~ ERROR [-, o]
56     x: &'a mut extern "Rust" fn(&'b isize),
57 }
58
59 // No uses at all is bivariant:
60
61 #[rustc_variance]
62 struct Test7<'a> { //~ ERROR [*]
63     //~^ ERROR parameter `'a` is never used
64     x: isize
65 }
66
67 // Try enums too.
68
69 #[rustc_variance]
70 enum Test8<'a, 'b, 'c:'b> { //~ ERROR [+, -, o]
71     Test8A(extern "Rust" fn(&'a isize)),
72     Test8B(&'b [isize]),
73     Test8C(&'b mut &'c str),
74 }
75
76 fn main() {}