rust/src/test/compile-fail/borrowck-lend-flow.rs
Niko Matsakis 5d32d03b89 Fix #2979: inference for lifetimes of & expressions
What we now do is to create a region variable for each &
expression (and also each borrow).  The lifetime of this
variable will be checked by borrowck to ensure it is not greater
than the lifetime of the underlying data.  This both leads to
shorter lifetimes in some cases but also longer in others,
such as taking the address to the interior of unique boxes
tht are rooted in region pointers (e.g., returning a pointer
to the interior of a sendable map).

This may lead to issue #2977 if the rvalue is not POD, because
we may drop the data in trans sooner than borrowck expects us
to.  Need to work out precisely where that fix ought to occur.
2012-07-30 14:49:28 -07:00

101 lines
2.8 KiB
Rust

// Note: the borrowck analysis is currently flow-insensitive.
// Therefore, some of these errors are marked as spurious and could be
// corrected by a simple change to the analysis. The others are
// either genuine or would require more advanced changes. The latter
// cases are noted.
fn borrow(_v: &int) {}
fn inc(v: &mut ~int) {
*v = ~(**v + 1);
}
fn post_aliased_const() {
let mut v = ~3;
borrow(v);
let _w = &const v;
}
fn post_aliased_mut() {
// SPURIOUS--flow
let mut v = ~3;
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
let _w = &mut v; //~ NOTE prior loan as mutable granted here
}
fn post_aliased_scope(cond: bool) {
let mut v = ~3;
borrow(v);
if cond { inc(&mut v); }
}
fn loop_overarching_alias_mut() {
let mut v = ~3;
let mut _x = &mut v; //~ NOTE prior loan as mutable granted here
loop {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
}
}
fn block_overarching_alias_mut() {
let mut v = ~3;
let mut _x = &mut v; //~ NOTE prior loan as mutable granted here
for 3.times {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
}
}
fn loop_aliased_mut() {
let mut v = ~3, w = ~4;
let mut _x = &mut w;
loop {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
_x = &mut v; //~ NOTE prior loan as mutable granted here
}
}
fn while_aliased_mut(cond: bool) {
let mut v = ~3, w = ~4;
let mut _x = &mut w;
while cond {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
_x = &mut v; //~ NOTE prior loan as mutable granted here
}
}
fn while_aliased_mut_cond(cond: bool, cond2: bool) {
let mut v = ~3, w = ~4;
let mut _x = &mut w;
while cond {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
if cond2 {
_x = &mut v; //~ NOTE prior loan as mutable granted here
}
}
}
fn loop_in_block() {
let mut v = ~3, w = ~4;
let mut _x = &mut w;
for uint::range(0u, 10u) |_i| {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
_x = &mut v; //~ NOTE prior loan as mutable granted here
}
}
fn at_most_once_block() {
fn at_most_once(f: fn()) { f() }
// Here, the borrow check has no way of knowing that the block is
// executed at most once.
let mut v = ~3, w = ~4;
let mut _x = &mut w;
do at_most_once {
borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
_x = &mut v; //~ NOTE prior loan as mutable granted here
}
}
fn main() {}