]> git.proxmox.com Git - rustc.git/blob - src/test/ui/nll/issue-27282-mutate-before-diverging-arm-2.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / test / ui / nll / issue-27282-mutate-before-diverging-arm-2.rs
1 // This is testing an attempt to corrupt the discriminant of the match
2 // arm in a guard, followed by an attempt to continue matching on that
3 // corrupted discriminant in the remaining match arms.
4 //
5 // Basically this is testing that our new NLL feature of emitting a
6 // fake read on each match arm is catching cases like this.
7 //
8 // This case is interesting because it includes a guard that
9 // diverges, and therefore a single final fake-read at the very end
10 // after the final match arm would not suffice.
11 //
12 // It is also interesting because the access to the corrupted data
13 // occurs in the pattern-match itself, and not in the guard
14 // expression.
15
16 struct ForceFnOnce;
17
18 fn main() {
19 let mut x = &mut Some(&2);
20 let force_fn_once = ForceFnOnce;
21 match x {
22 &mut None => panic!("unreachable"),
23 &mut Some(&_)
24 if {
25 // ForceFnOnce needed to exploit #27282
26 (|| { *x = None; drop(force_fn_once); })();
27 //~^ ERROR cannot mutably borrow `x` in match guard [E0510]
28 false
29 } => {}
30
31 // this segfaults if we corrupted the discriminant, because
32 // the compiler gets to *assume* that it cannot be the `None`
33 // case, even though that was the effect of the guard.
34 &mut Some(&2)
35 if {
36 panic!()
37 } => {}
38 _ => panic!("unreachable"),
39 }
40 }