]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/two-phase-allow-access-during-reservation.rs
Rollup merge of #82259 - osa1:issue82156, r=petrochenkov
[rust.git] / src / test / ui / borrowck / two-phase-allow-access-during-reservation.rs
1 // ignore-tidy-linelength
2
3 // revisions: nll_target
4
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
7
8 //[nll_target] compile-flags: -Z borrowck=mir
9
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/
12 //
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.
16 //
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
23 // corner cases).
24
25 fn main() {
26     /*0*/ let mut i = 0;
27
28     /*1*/ let p = &mut i; // (reservation of `i` starts here)
29
30     /*2*/ let j = i;      // OK: `i` is only reserved here
31                           //[nll_target]~^  ERROR cannot use `i` because it was mutably borrowed [E0503]
32
33     /*3*/ *p += 1;        // (mutable borrow of `i` starts here, since `p` is used)
34
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]
37
38     /*5*/ *p += 1;
39
40     let _ = (j, k, p);
41 }