]> git.proxmox.com Git - rustc.git/blob - src/test/ui/borrowck/two-phase-allow-access-during-reservation.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / test / ui / borrowck / two-phase-allow-access-during-reservation.rs
1 // revisions: nll_target
2
3 // The following revisions are disabled due to missing support for two_phase_beyond_autoref
4 //[nll_beyond] compile-flags: -Z two_phase_beyond_autoref
5
6 // This is the second counter-example from Niko's blog post
7 // smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
8 //
9 // It is "artificial". It is meant to illustrate directly that we
10 // should allow an aliasing access during reservation, but *not* while
11 // the mutable borrow is active.
12 //
13 // The convention for the listed revisions: "lxl" means lexical
14 // lifetimes (which can be easier to reason about). "nll" means
15 // non-lexical lifetimes. "nll_target" means the initial conservative
16 // two-phase borrows that only applies to autoref-introduced borrows.
17 // "nll_beyond" means the generalization of two-phase borrows to all
18 // `&mut`-borrows (doing so makes it easier to write code for specific
19 // corner cases).
20
21 fn main() {
22 /*0*/ let mut i = 0;
23
24 /*1*/ let p = &mut i; // (reservation of `i` starts here)
25
26 /*2*/ let j = i; // OK: `i` is only reserved here
27 //[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
28
29 /*3*/ *p += 1; // (mutable borrow of `i` starts here, since `p` is used)
30
31 /*4*/ let k = i; //[nll_beyond]~ ERROR cannot use `i` because it was mutably borrowed [E0503]
32 //[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
33
34 /*5*/ *p += 1;
35
36 let _ = (j, k, p);
37 }