]> git.lizzy.rs Git - rust.git/blob - src/test/ui/hr-subtype/hr-subtype.rs
rewrite leak check to be based on universes
[rust.git] / src / test / ui / hr-subtype / hr-subtype.rs
1 // Targeted tests for the higher-ranked subtyping code.
2
3 #![feature(rustc_attrs)]
4 #![allow(dead_code)]
5
6 // revisions: bound_a_vs_bound_a
7 // revisions: bound_a_vs_bound_b
8 // revisions: bound_inv_a_vs_bound_inv_b
9 // revisions: bound_co_a_vs_bound_co_b
10 // revisions: bound_a_vs_free_x
11 // revisions: free_x_vs_free_x
12 // revisions: free_x_vs_free_y
13 // revisions: free_inv_x_vs_free_inv_y
14 // revisions: bound_a_b_vs_bound_a
15 // revisions: bound_co_a_b_vs_bound_co_a
16 // revisions: bound_contra_a_contra_b_ret_co_a
17 // revisions: bound_co_a_co_b_ret_contra_a
18 // revisions: bound_inv_a_b_vs_bound_inv_a
19 // revisions: bound_a_b_ret_a_vs_bound_a_ret_a
20
21 fn gimme<T>(_: Option<T>) {}
22
23 struct Inv<'a> {
24     x: *mut &'a u32,
25 }
26
27 struct Co<'a> {
28     x: fn(&'a u32),
29 }
30
31 struct Contra<'a> {
32     x: &'a u32,
33 }
34
35 macro_rules! check {
36     ($rev:ident: ($t1:ty, $t2:ty)) => {
37         #[cfg($rev)]
38         fn subtype<'x, 'y: 'x, 'z: 'y>() {
39             gimme::<$t2>(None::<$t1>);
40             //[free_inv_x_vs_free_inv_y]~^ ERROR
41         }
42
43         #[cfg($rev)]
44         fn supertype<'x, 'y: 'x, 'z: 'y>() {
45             gimme::<$t1>(None::<$t2>);
46             //[bound_a_vs_free_x]~^ ERROR
47             //[free_x_vs_free_y]~^^ ERROR
48             //[bound_inv_a_b_vs_bound_inv_a]~^^^ ERROR
49             //[bound_a_b_ret_a_vs_bound_a_ret_a]~^^^^ ERROR
50             //[free_inv_x_vs_free_inv_y]~^^^^^ ERROR
51             //[bound_a_b_vs_bound_a]~^^^^^^ ERROR mismatched types
52             //[bound_contra_a_contra_b_ret_co_a]~^^^^^^^ ERROR
53         }
54     };
55 }
56
57 // If both have bound regions, they are equivalent, regardless of
58 // variant.
59 check! { bound_a_vs_bound_a: (for<'a> fn(&'a u32),
60 for<'a> fn(&'a u32)) }
61 check! { bound_a_vs_bound_b: (for<'a> fn(&'a u32),
62 for<'b> fn(&'b u32)) }
63 check! { bound_inv_a_vs_bound_inv_b: (for<'a> fn(Inv<'a>),
64 for<'b> fn(Inv<'b>)) }
65 check! { bound_co_a_vs_bound_co_b: (for<'a> fn(Co<'a>),
66 for<'b> fn(Co<'b>)) }
67
68 // Bound is a subtype of free.
69 check! { bound_a_vs_free_x: (for<'a> fn(&'a u32),
70 fn(&'x u32)) }
71
72 // Two free regions are relatable if subtyping holds.
73 check! { free_x_vs_free_x: (fn(&'x u32),
74 fn(&'x u32)) }
75 check! { free_x_vs_free_y: (fn(&'x u32),
76 fn(&'y u32)) }
77 check! { free_inv_x_vs_free_inv_y: (fn(Inv<'x>),
78 fn(Inv<'y>)) }
79
80 // Somewhat surprisingly, a fn taking two distinct bound lifetimes and
81 // a fn taking one bound lifetime can be interchangeable, but only if
82 // we are co- or contra-variant with respect to both lifetimes.
83 //
84 // The reason is:
85 // - if we are covariant, then 'a and 'b can be set to the call-site
86 //   intersection;
87 // - if we are contravariant, then 'a can be inferred to 'static.
88 check! { bound_a_b_vs_bound_a: (for<'a,'b> fn(&'a u32, &'b u32),
89 for<'a>    fn(&'a u32, &'a u32)) }
90 check! { bound_co_a_b_vs_bound_co_a: (for<'a,'b> fn(Co<'a>, Co<'b>),
91 for<'a>    fn(Co<'a>, Co<'a>)) }
92 check! { bound_contra_a_contra_b_ret_co_a: (for<'a,'b> fn(Contra<'a>, Contra<'b>) -> Co<'a>,
93 for<'a>    fn(Contra<'a>, Contra<'a>) -> Co<'a>) }
94 check! { bound_co_a_co_b_ret_contra_a: (for<'a,'b> fn(Co<'a>, Co<'b>) -> Contra<'a>,
95 for<'a>    fn(Co<'a>, Co<'a>) -> Contra<'a>) }
96
97 // If we make those lifetimes invariant, then the two types are not interchangeable.
98 check! { bound_inv_a_b_vs_bound_inv_a: (for<'a,'b> fn(Inv<'a>, Inv<'b>),
99 for<'a>    fn(Inv<'a>, Inv<'a>)) }
100 check! { bound_a_b_ret_a_vs_bound_a_ret_a: (for<'a,'b> fn(&'a u32, &'b u32) -> &'a u32,
101 for<'a>    fn(&'a u32, &'a u32) -> &'a u32) }
102
103 #[rustc_error]
104 fn main() {
105     //[bound_a_vs_bound_a]~^ ERROR fatal error triggered by #[rustc_error]
106     //[bound_a_vs_bound_b]~^^ ERROR fatal error triggered by #[rustc_error]
107     //[bound_inv_a_vs_bound_inv_b]~^^^ ERROR fatal error triggered by #[rustc_error]
108     //[bound_co_a_vs_bound_co_b]~^^^^ ERROR fatal error triggered by #[rustc_error]
109     //[free_x_vs_free_x]~^^^^^ ERROR fatal error triggered by #[rustc_error]
110     //[bound_co_a_b_vs_bound_co_a]~^^^^^^ ERROR
111     //[bound_co_a_co_b_ret_contra_a]~^^^^^^^ ERROR
112 }