Get Polonius borrow check to work in simple cases

* Restores the generation of outlives facts from subtyping.
* Restore liveness facts.
* Generate invalidates facts at the start point of each location,
  where we check for errors.
* Add a small test for simple cases.
This commit is contained in:
Matthew Jasper 2018-09-26 20:41:14 +01:00
parent c7df1f530b
commit 610903fb11
10 changed files with 172 additions and 80 deletions

View file

@ -0,0 +1,47 @@
// Check that Polonius borrow check works for simple cases.
// ignore-compare-mode-nll
// compile-flags: -Z borrowck=mir -Zpolonius
pub fn return_ref_to_local() -> &'static i32 {
let x = 0;
&x //~ ERROR
}
pub fn use_while_mut() {
let mut x = 0;
let y = &mut x;
let z = x; //~ ERROR
let w = y;
}
pub fn use_while_mut_fr(x: &mut i32) -> &mut i32 {
let y = &mut *x;
let z = x; //~ ERROR
y
}
// Cases like this are why we have Polonius.
pub fn position_dependent_outlives(x: &mut i32, cond: bool) -> &mut i32 {
let y = &mut *x;
if cond {
return y;
} else {
*x = 0;
return x;
}
}
fn foo<'a, 'b>(p: &'b &'a mut usize) -> &'b usize {
p
}
// Check that we create constraints for well-formedness of function arguments
fn well_formed_function_inputs() {
let s = &mut 1;
let r = &mut *s;
let tmp = foo(&r);
s; //~ ERROR
tmp;
}
fn main() {}

View file

@ -0,0 +1,45 @@
error[E0597]: `x` does not live long enough
--> $DIR/polonius-smoke-test.rs:7:5
|
LL | &x //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...
error[E0503]: cannot use `x` because it was mutably borrowed
--> $DIR/polonius-smoke-test.rs:13:13
|
LL | let y = &mut x;
| ------ borrow of `x` occurs here
LL | let z = x; //~ ERROR
| ^ use of borrowed `x`
LL | let w = y;
| - borrow later used here
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/polonius-smoke-test.rs:19:13
|
LL | let y = &mut *x;
| ------- borrow of `*x` occurs here
LL | let z = x; //~ ERROR
| ^ move out of `x` occurs here
LL | y
| - borrow later used here
error[E0505]: cannot move out of `s` because it is borrowed
--> $DIR/polonius-smoke-test.rs:43:5
|
LL | let r = &mut *s;
| ------- borrow of `*s` occurs here
LL | let tmp = foo(&r);
LL | s; //~ ERROR
| ^ move out of `s` occurs here
LL | tmp;
| --- borrow later used here
error: aborting due to 4 previous errors
Some errors occurred: E0503, E0505, E0597.
For more information about an error, try `rustc --explain E0503`.