2013-10-29 05:08:34 -05: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.
|
|
|
|
|
|
|
|
// Regions that just appear in normal spots are contravariant:
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test2<'a, 'b, 'c> { //~ ERROR regions=[[-, -, -];[];[]]
|
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]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test3<'a, 'b, 'c> { //~ ERROR regions=[[+, +, +];[];[]]
|
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]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test4<'a, 'b:'a> { //~ ERROR regions=[[-, 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]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test5<'a, 'b> { //~ ERROR regions=[[+, 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
|
2013-10-29 05:08:34 -05:00
|
|
|
// a argument list (which is contravariant), that
|
|
|
|
// argument list occurs in an invariant context.
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test6<'a, 'b> { //~ ERROR regions=[[-, 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]
|
2014-12-29 15:32:12 -06:00
|
|
|
struct Test7<'a> { //~ ERROR regions=[[*];[];[]]
|
2015-01-08 04:54:35 -06:00
|
|
|
x: isize
|
2013-10-29 05:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try enums too.
|
|
|
|
|
|
|
|
#[rustc_variance]
|
2014-12-29 15:32:12 -06:00
|
|
|
enum Test8<'a, 'b, 'c:'b> { //~ ERROR regions=[[+, -, 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() {}
|