]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-27282-mutate-before-diverging-arm-1.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / issues / issue-27282-mutate-before-diverging-arm-1.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This is testing an attempt to corrupt the discriminant of the match
12 // arm in a guard, followed by an attempt to continue matching on that
13 // corrupted discriminant in the remaining match arms.
14 //
15 // Basically this is testing that our new NLL feature of emitting a
16 // fake read on each match arm is catching cases like this.
17 //
18 // This case is interesting because it includes a guard that
19 // diverges, and therefore a single final fake-read at the very end
20 // after the final match arm would not suffice.
21
22 #![feature(nll)]
23
24 struct ForceFnOnce;
25
26 fn main() {
27     let mut x = &mut Some(&2);
28     let force_fn_once = ForceFnOnce;
29     match x {
30         &mut None => panic!("unreachable"),
31         &mut Some(&_) if {
32             // ForceFnOnce needed to exploit #27282
33             (|| { *x = None; drop(force_fn_once); })();
34             //~^ ERROR cannot mutably borrow `x` in match guard [E0510]
35             false
36         } => {}
37         &mut Some(&a) if { // this binds to garbage if we've corrupted discriminant
38             println!("{}", a);
39             panic!()
40         } => {}
41         _ => panic!("unreachable"),
42     }
43 }