]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/two-phase-reservation-sharing-interference.rs
Sync portable-simd to rust-lang/portable-simd@72df4c45056a8bc0d1b3f06fdc828722177f0763
[rust.git] / src / test / 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 borrowck=mir -Z two-phase-beyond-autoref
5 //[nll_beyond]should-fail
6
7 //[nll_target]compile-flags: -Z borrowck=mir
8
9 // This is a corner case that the current implementation is (probably)
10 // treating more conservatively than is necessary. But it also does
11 // not seem like a terribly important use case to cover.
12 //
13 // So this test is just making a note of the current behavior, with
14 // the caveat that in the future, the rules may be loosened, at which
15 // point this test might be thrown out.
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     let mut vec = vec![0, 1];
27     let delay: &mut Vec<_>;
28     {
29         let shared = &vec;
30
31         // we reserve here, which could (on its own) be compatible
32         // with the shared borrow. But in the current implementation,
33         // its an error.
34         delay = &mut vec;
35         //[nll_beyond]~^  ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
36         //[nll_target]~^^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
37
38         shared[0];
39     }
40
41     // the &mut-borrow only becomes active way down here.
42     //
43     // (At least in theory; part of the reason this test fails is that
44     // the constructed MIR throws in extra &mut reborrows which
45     // flummoxes our attmpt to delay the activation point here.)
46     delay.push(2);
47 }