]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/two-phase-reservation-sharing-interference.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / borrowck / two-phase-reservation-sharing-interference.rs
1 // ignore-tidy-linelength
2
3 // revisions: nll_target
4
5 // The following revisions are disabled due to missing support from two-phase beyond autorefs
6 //[nll_beyond]compile-flags: -Z borrowck=mir -Z two-phase-beyond-autoref
7 //[nll_beyond] should-fail
8
9 //[nll_target]compile-flags: -Z borrowck=mir
10
11 // This is a corner case that the current implementation is (probably)
12 // treating more conservatively than is necessary. But it also does
13 // not seem like a terribly important use case to cover.
14 //
15 // So this test is just making a note of the current behavior, with
16 // the caveat that in the future, the rules may be loosened, at which
17 // point this test might be thrown out.
18 //
19 // The convention for the listed revisions: "lxl" means lexical
20 // lifetimes (which can be easier to reason about). "nll" means
21 // non-lexical lifetimes. "nll_target" means the initial conservative
22 // two-phase borrows that only applies to autoref-introduced borrows.
23 // "nll_beyond" means the generalization of two-phase borrows to all
24 // `&mut`-borrows (doing so makes it easier to write code for specific
25 // corner cases).
26
27 fn main() {
28     let mut vec = vec![0, 1];
29     let delay: &mut Vec<_>;
30     {
31         let shared = &vec;
32
33         // we reserve here, which could (on its own) be compatible
34         // with the shared borrow. But in the current implementation,
35         // its an error.
36         delay = &mut vec;
37         //[nll_beyond]~^  ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
38         //[nll_target]~^^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
39
40         shared[0];
41     }
42
43     // the &mut-borrow only becomes active way down here.
44     //
45     // (At least in theory; part of the reason this test fails is that
46     // the constructed MIR throws in extra &mut reborrows which
47     // flummoxes our attmpt to delay the activation point here.)
48     delay.push(2);
49 }