]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-27282-mutate-before-diverging-arm-2.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / issues / issue-27282-mutate-before-diverging-arm-2.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 // It is also interesting because the access to the corrupted data
23 // occurs in the pattern-match itself, and not in the guard
24 // expression.
25
26 #![feature(nll)]
27
28 struct ForceFnOnce;
29
30 fn main() {
31     let mut x = &mut Some(&2);
32     let force_fn_once = ForceFnOnce;
33     match x {
34         &mut None => panic!("unreachable"),
35         &mut Some(&_)
36             if {
37                 // ForceFnOnce needed to exploit #27282
38                 (|| { *x = None; drop(force_fn_once); })();
39                 //~^ ERROR cannot mutably borrow `x` in match guard [E0510]
40                 false
41             } => {}
42
43         // this segfaults if we corrupted the discriminant, because
44         // the compiler gets to *assume* that it cannot be the `None`
45         // case, even though that was the effect of the guard.
46         &mut Some(&2)
47             if {
48                 panic!()
49             } => {}
50         _ => panic!("unreachable"),
51     }
52 }