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