1 // ignore-tidy-linelength
3 // revisions: nll_target
5 // The following revisions are disabled due to missing support for two_phase_beyond_autoref
6 //[nll_beyond] compile-flags: -Z borrowck=mir -Z two_phase_beyond_autoref
8 //[nll_target] compile-flags: -Z borrowck=mir
10 // This is the second counter-example from Niko's blog post
11 // smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
13 // It is "artificial". It is meant to illustrate directly that we
14 // should allow an aliasing access during reservation, but *not* while
15 // the mutable borrow is active.
17 // The convention for the listed revisions: "lxl" means lexical
18 // lifetimes (which can be easier to reason about). "nll" means
19 // non-lexical lifetimes. "nll_target" means the initial conservative
20 // two-phase borrows that only applies to autoref-introduced borrows.
21 // "nll_beyond" means the generalization of two-phase borrows to all
22 // `&mut`-borrows (doing so makes it easier to write code for specific
28 /*1*/ let p = &mut i; // (reservation of `i` starts here)
30 /*2*/ let j = i; // OK: `i` is only reserved here
31 //[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
33 /*3*/ *p += 1; // (mutable borrow of `i` starts here, since `p` is used)
35 /*4*/ let k = i; //[nll_beyond]~ ERROR cannot use `i` because it was mutably borrowed [E0503]
36 //[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]