2018-03-05 01:44:10 -06:00
|
|
|
// revisions: nll_target
|
|
|
|
|
|
|
|
// The following revisions are disabled due to missing support for two_phase_beyond_autoref
|
2022-04-01 12:13:25 -05:00
|
|
|
//[nll_beyond] compile-flags: -Z two_phase_beyond_autoref
|
2017-12-05 17:37:06 -06:00
|
|
|
|
|
|
|
// This is the second counter-example from Niko's blog post
|
|
|
|
// smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
|
|
|
|
//
|
|
|
|
// It is "artificial". It is meant to illustrate directly that we
|
|
|
|
// should allow an aliasing access during reservation, but *not* while
|
|
|
|
// the mutable borrow is active.
|
2018-01-16 03:52:52 -06:00
|
|
|
//
|
|
|
|
// The convention for the listed revisions: "lxl" means lexical
|
|
|
|
// lifetimes (which can be easier to reason about). "nll" means
|
|
|
|
// non-lexical lifetimes. "nll_target" means the initial conservative
|
|
|
|
// two-phase borrows that only applies to autoref-introduced borrows.
|
|
|
|
// "nll_beyond" means the generalization of two-phase borrows to all
|
|
|
|
// `&mut`-borrows (doing so makes it easier to write code for specific
|
|
|
|
// corner cases).
|
2017-12-05 17:37:06 -06:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
/*0*/ let mut i = 0;
|
|
|
|
|
|
|
|
/*1*/ let p = &mut i; // (reservation of `i` starts here)
|
|
|
|
|
|
|
|
/*2*/ let j = i; // OK: `i` is only reserved here
|
2018-01-16 03:52:52 -06:00
|
|
|
//[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
|
2017-12-05 17:37:06 -06:00
|
|
|
|
|
|
|
/*3*/ *p += 1; // (mutable borrow of `i` starts here, since `p` is used)
|
|
|
|
|
2018-04-09 04:28:00 -05:00
|
|
|
/*4*/ let k = i; //[nll_beyond]~ ERROR cannot use `i` because it was mutably borrowed [E0503]
|
|
|
|
//[nll_target]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
|
2017-12-05 17:37:06 -06:00
|
|
|
|
|
|
|
/*5*/ *p += 1;
|
|
|
|
|
|
|
|
let _ = (j, k, p);
|
|
|
|
}
|