2013-10-29 10:08:34 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// 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]
|
2016-08-17 03:32:00 +00:00
|
|
|
struct Test2<'a, 'b, 'c> { //~ ERROR regions=[-, -, -]
|
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]
|
2016-08-17 03:32:00 +00:00
|
|
|
struct Test3<'a, 'b, 'c> { //~ ERROR regions=[+, +, +]
|
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]
|
2016-08-17 03:32:00 +00:00
|
|
|
struct Test4<'a, 'b:'a> { //~ ERROR regions=[-, 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]
|
2016-08-17 03:32:00 +00:00
|
|
|
struct Test5<'a, 'b:'a> { //~ ERROR regions=[+, 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]
|
2016-08-17 03:32:00 +00:00
|
|
|
struct Test6<'a, 'b:'a> { //~ ERROR regions=[-, 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-17 03:32:00 +00:00
|
|
|
struct Test7<'a> { //~ ERROR regions=[*]
|
2015-02-12 15:29:52 +00:00
|
|
|
//~^ ERROR parameter `'a` is never used
|
2015-01-08 10:54:35 +00:00
|
|
|
x: isize
|
2013-10-29 10:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try enums too.
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2016-08-17 03:32:00 +00:00
|
|
|
enum Test8<'a, 'b, 'c:'b> { //~ ERROR regions=[+, -, 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() {}
|