]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
85aaf69f SL |
14 | #![feature(rustc_attrs)] |
15 | ||
1a4d82fc JJ |
16 | // Regions that just appear in normal spots are contravariant: |
17 | ||
18 | #[rustc_variance] | |
9e0c209e | 19 | struct Test2<'a, 'b, 'c> { //~ ERROR [-, -, -] |
1a4d82fc JJ |
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] | |
9e0c209e | 28 | struct Test3<'a, 'b, 'c> { //~ ERROR [+, +, +] |
1a4d82fc JJ |
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] | |
9e0c209e | 37 | struct Test4<'a, 'b:'a> { //~ ERROR [-, o] |
1a4d82fc JJ |
38 | x: &'a mut &'b isize, |
39 | } | |
40 | ||
41 | // Mutability induces invariance, even when in a | |
42 | // contravariant context: | |
43 | ||
44 | #[rustc_variance] | |
9e0c209e | 45 | struct Test5<'a, 'b:'a> { //~ ERROR [+, o] |
1a4d82fc JJ |
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 | |
b039eaaf | 51 | // an argument list (which is contravariant), that |
1a4d82fc JJ |
52 | // argument list occurs in an invariant context. |
53 | ||
54 | #[rustc_variance] | |
9e0c209e | 55 | struct Test6<'a, 'b:'a> { //~ ERROR [-, o] |
1a4d82fc JJ |
56 | x: &'a mut extern "Rust" fn(&'b isize), |
57 | } | |
58 | ||
59 | // No uses at all is bivariant: | |
60 | ||
61 | #[rustc_variance] | |
9e0c209e | 62 | struct Test7<'a> { //~ ERROR [*] |
85aaf69f | 63 | //~^ ERROR parameter `'a` is never used |
1a4d82fc JJ |
64 | x: isize |
65 | } | |
66 | ||
67 | // Try enums too. | |
68 | ||
69 | #[rustc_variance] | |
9e0c209e | 70 | enum Test8<'a, 'b, 'c:'b> { //~ ERROR [+, -, o] |
1a4d82fc JJ |
71 | Test8A(extern "Rust" fn(&'a isize)), |
72 | Test8B(&'b [isize]), | |
73 | Test8C(&'b mut &'c str), | |
74 | } | |
75 | ||
76 | fn main() {} |