2013-10-29 05:08:34 -05:00
|
|
|
// Test that we correctly infer variance for region parameters in
|
|
|
|
// various self-contained types.
|
|
|
|
|
2015-02-16 14:21:41 -06:00
|
|
|
#![feature(rustc_attrs)]
|
|
|
|
|
2013-10-29 05:08:34 -05:00
|
|
|
// Regions that just appear in normal spots are contravariant:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
struct Test2<'a, 'b, 'c> { //~ ERROR [+, +, +]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: &'a isize,
|
|
|
|
y: &'b [isize],
|
2013-10-29 05:08:34 -05:00
|
|
|
c: &'c str
|
|
|
|
}
|
|
|
|
|
|
|
|
// Those same annotations in function arguments become covariant:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
struct Test3<'a, 'b, 'c> { //~ ERROR [-, -, -]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: extern "Rust" fn(&'a isize),
|
|
|
|
y: extern "Rust" fn(&'b [isize]),
|
2013-10-29 05:08:34 -05:00
|
|
|
c: extern "Rust" fn(&'c str),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutability induces invariance:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
struct Test4<'a, 'b:'a> { //~ ERROR [+, o]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: &'a mut &'b isize,
|
2013-10-29 05:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mutability induces invariance, even when in a
|
|
|
|
// contravariant context:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
struct Test5<'a, 'b:'a> { //~ ERROR [-, o]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: extern "Rust" fn(&'a mut &'b isize),
|
2013-10-29 05:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invariance is a trap from which NO ONE CAN ESCAPE.
|
2015-01-08 04:54:35 -06:00
|
|
|
// In other words, even though the `&'b isize` occurs in
|
2015-10-07 17:11:25 -05:00
|
|
|
// an argument list (which is contravariant), that
|
2013-10-29 05:08:34 -05:00
|
|
|
// argument list occurs in an invariant context.
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
struct Test6<'a, 'b:'a> { //~ ERROR [+, o]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: &'a mut extern "Rust" fn(&'b isize),
|
2013-10-29 05:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// No uses at all is bivariant:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2016-08-26 17:13:48 -05:00
|
|
|
struct Test7<'a> { //~ ERROR [*]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: isize
|
2013-10-29 05:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try enums too.
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2023-01-26 17:23:08 -06:00
|
|
|
enum Test8<'a, 'b, 'c:'b> { //~ ERROR [-, +, o]
|
2015-01-08 04:54:35 -06:00
|
|
|
Test8A(extern "Rust" fn(&'a isize)),
|
|
|
|
Test8B(&'b [isize]),
|
2013-10-29 05:08:34 -05:00
|
|
|
Test8C(&'b mut &'c str),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|