Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check activation points as the place where mutable borrows become rel…
…evant. Since we are now checking activation points, I removed one of the checks at the reservation point. (You can see the effect this had on two-phase-reservation-sharing-interference-2.rs) Also, since we now have checks at both the reservation point and the activation point, we sometimes would observe duplicate errors (since either one independently interferes with another mutable borrow). To deal with this, I used a similar strategy to one used as discussed on issue #45360: keep a set of errors reported (in this case for reservations), and then avoid doing the checks for the corresponding activations. (This does mean that some errors could get masked, namely for conflicting borrows that start after the reservation but still conflict with the activation, which is unchecked when there was an error for the reservation. But this seems like a reasonable price to pay.)
- Loading branch information
Showing
3 changed files
with
192 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/compile-fail/borrowck/two-phase-activation-sharing-interference.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// revisions: lxl nll | ||
//[lxl]compile-flags: -Z borrowck=mir -Z two-phase-borrows | ||
//[nll]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll | ||
|
||
// This is an important corner case pointed out by Niko: one is | ||
// allowed to initiate a shared borrow during a reservation, but it | ||
// *must end* before the activation occurs. | ||
// | ||
// FIXME: for clarity, diagnostics for these cases might be better off | ||
// if they specifically said "cannot activate mutable borrow of `x`" | ||
|
||
#![allow(dead_code)] | ||
|
||
fn read(_: &i32) { } | ||
|
||
fn ok() { | ||
let mut x = 3; | ||
let y = &mut x; | ||
{ let z = &x; read(z); } | ||
*y += 1; | ||
} | ||
|
||
fn not_ok() { | ||
let mut x = 3; | ||
let y = &mut x; | ||
let z = &x; | ||
*y += 1; | ||
//[lxl]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | ||
//[nll]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | ||
read(z); | ||
} | ||
|
||
fn should_be_ok_with_nll() { | ||
let mut x = 3; | ||
let y = &mut x; | ||
let z = &x; | ||
read(z); | ||
*y += 1; | ||
//[lxl]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | ||
// (okay with nll today) | ||
} | ||
|
||
fn should_also_eventually_be_ok_with_nll() { | ||
let mut x = 3; | ||
let y = &mut x; | ||
let _z = &x; | ||
*y += 1; | ||
//[lxl]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | ||
//[nll]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | ||
} | ||
|
||
fn main() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters