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