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