Add tests for new match borrows

This commit is contained in:
Matthew Jasper 2018-09-13 22:04:00 +01:00
parent 46e247bcec
commit c5047cb494
6 changed files with 457 additions and 0 deletions

View file

@ -0,0 +1,30 @@
// This is testing an attempt to corrupt the discriminant of the match
// arm in a guard, followed by an attempt to continue matching on that
// corrupted discriminant in the remaining match arms.
//
// Basically this is testing that our new NLL feature of emitting a
// fake read on each match arm is catching cases like this.
//
// This case is interesting because a borrow of **x is untracked, because **x is
// immutable. However, for matches we care that **x refers to the same value
// until we have chosen a match arm.
#![feature(nll)]
struct ForceFnOnce;
fn main() {
let mut x = &mut &Some(&2);
let force_fn_once = ForceFnOnce;
match **x {
None => panic!("unreachable"),
Some(&_) if {
// ForceFnOnce needed to exploit #27282
(|| { *x = &None; drop(force_fn_once); })();
//~^ ERROR cannot mutably borrow `x` in match guard [E0510]
false
} => {}
Some(&a) if { // this binds to garbage if we've corrupted discriminant
println!("{}", a);
panic!()
} => {}
_ => panic!("unreachable"),
}
}

View file

@ -0,0 +1,14 @@
error[E0510]: cannot mutably borrow `x` in match guard
--> $DIR/issue-27282-mutate-before-diverging-arm-3.rs:20:14
|
LL | match **x {
| --- value is immutable in match guard
...
LL | (|| { *x = &None; drop(force_fn_once); })();
| ^^ - borrow occurs due to use of `x` in closure
| |
| cannot mutably borrow
error: aborting due to previous error
For more information about this error, try `rustc --explain E0510`.