1 // Targeted tests for the higher-ranked subtyping code.
3 #![feature(rustc_attrs)]
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
21 fn gimme<T>(_: Option<T>) {}
36 ($rev:ident: ($t1:ty, $t2:ty)) => {
38 fn subtype<'x, 'y: 'x, 'z: 'y>() {
39 gimme::<$t2>(None::<$t1>);
40 //[free_inv_x_vs_free_inv_y]~^ ERROR
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
57 // If both have bound regions, they are equivalent, regardless of
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>),
68 // Bound is a subtype of free.
69 check! { bound_a_vs_free_x: (for<'a> fn(&'a u32),
72 // Two free regions are relatable if subtyping holds.
73 check! { free_x_vs_free_x: (fn(&'x u32),
75 check! { free_x_vs_free_y: (fn(&'x u32),
77 check! { free_inv_x_vs_free_inv_y: (fn(Inv<'x>),
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.
85 // - if we are covariant, then 'a and 'b can be set to the call-site
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>) }
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) }
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