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