]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/two-phase-allow-access-during-reservation.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / two-phase-allow-access-during-reservation.rs
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.
4 //
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.
10
11 // ignore-tidy-linelength
12
13 // revisions: nll_target
14
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
17
18 //[nll_target] compile-flags: -Z borrowck=mir -Z two-phase-borrows
19
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/
22 //
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.
26 //
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
33 // corner cases).
34
35 fn main() {
36     /*0*/ let mut i = 0;
37
38     /*1*/ let p = &mut i; // (reservation of `i` starts here)
39
40     /*2*/ let j = i;      // OK: `i` is only reserved here
41                           //[nll_target]~^  ERROR cannot use `i` because it was mutably borrowed [E0503]
42
43     /*3*/ *p += 1;        // (mutable borrow of `i` starts here, since `p` is used)
44
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]
47
48     /*5*/ *p += 1;
49
50     let _ = (j, k, p);
51 }