|
| 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 | +// revisions: lxl nll |
| 12 | +//[lxl]compile-flags: -Z borrowck=mir -Z two-phase-borrows |
| 13 | +//[nll]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll |
| 14 | + |
| 15 | +// This is a corner case that the current implementation is (probably) |
| 16 | +// treating more conservatively than is necessary. But it also does |
| 17 | +// not seem like a terribly important use case to cover. |
| 18 | +// |
| 19 | +// So this test is just making a note of the current behavior, with |
| 20 | +// the caveat that in the future, the rules may be loosened, at which |
| 21 | +// point this test might be thrown out. |
| 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 | + //[lxl]~^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable |
| 34 | + //[nll]~^^ 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 attmpt to delay the activation point here.) |
| 44 | + delay.push(2); |
| 45 | +} |
| 46 | + |
0 commit comments