]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck/two-phase-reservation-sharing-interference.rs
Rollup merge of #48005 - panicbit:env_unimpl_send_sync, r=alexcrichton
[rust.git] / src / test / compile-fail / borrowck / two-phase-reservation-sharing-interference.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: lxl_beyond nll_beyond nll_target
14
15 //[lxl_beyond]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z two-phase-beyond-autoref
16 //[nll_beyond]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z two-phase-beyond-autoref -Z nll
17 //[nll_target]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll
18
19 // This is a corner case that the current implementation is (probably)
20 // treating more conservatively than is necessary. But it also does
21 // not seem like a terribly important use case to cover.
22 //
23 // So this test is just making a note of the current behavior, with
24 // the caveat that in the future, the rules may be loosened, at which
25 // point this test might be thrown out.
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 // FIXME: in "nll_target", we currently see the same error reported
36 // twice. This is injected by `-Z two-phase-borrows`; not sure why as
37 // of yet.
38
39 fn main() {
40     let mut vec = vec![0, 1];
41     let delay: &mut Vec<_>;
42     {
43         let shared = &vec;
44
45         // we reserve here, which could (on its own) be compatible
46         // with the shared borrow. But in the current implementation,
47         // its an error.
48         delay = &mut vec;
49         //[lxl_beyond]~^   ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
50         //[nll_beyond]~^^  ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
51         //[nll_target]~^^^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
52         //[nll_target]~|   ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
53
54         shared[0];
55     }
56
57     // the &mut-borrow only becomes active way down here.
58     //
59     // (At least in theory; part of the reason this test fails is that
60     // the constructed MIR throws in extra &mut reborrows which
61     // flummoxes our attmpt to delay the activation point here.)
62     delay.push(2);
63 }
64