Skip to content

Commit 056f589

Browse files
committed
Auto merge of #49678 - bobtwinkles:fix_multiple_activations, r=nikomatsakis
two-phase borrows: support multiple activations in one statement The need for this has arisen since the introduction of two-phase borrows on method autorefs in #49348. r'ing @pnkfelix to keep things off Niko's plate so he can make this redundant, and @pnkfelix is familiar with the code. Fixes #49635 Fixes #49662 r? @pnkfelix
2 parents 7807074 + bacd120 commit 056f589

File tree

4 files changed

+92
-14
lines changed

4 files changed

+92
-14
lines changed

src/librustc_mir/dataflow/impls/borrows.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
6464
assigned_map: FxHashMap<Place<'tcx>, FxHashSet<BorrowIndex>>,
6565

6666
/// Locations which activate borrows.
67-
/// NOTE: A given location may activate more than one borrow in the future
68-
/// when more general two-phase borrow support is introduced, but for now we
69-
/// only need to store one borrow index
70-
activation_map: FxHashMap<Location, BorrowIndex>,
67+
activation_map: FxHashMap<Location, FxHashSet<BorrowIndex>>,
7168

7269
/// Every borrow has a region; this maps each such regions back to
7370
/// its borrow-indexes.
@@ -174,7 +171,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
174171
idx_vec: IndexVec<BorrowIndex, BorrowData<'tcx>>,
175172
location_map: FxHashMap<Location, BorrowIndex>,
176173
assigned_map: FxHashMap<Place<'tcx>, FxHashSet<BorrowIndex>>,
177-
activation_map: FxHashMap<Location, BorrowIndex>,
174+
activation_map: FxHashMap<Location, FxHashSet<BorrowIndex>>,
178175
region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
179176
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
180177
region_span_map: FxHashMap<RegionKind, Span>,
@@ -211,12 +208,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
211208
let idx = self.idx_vec.push(borrow);
212209
self.location_map.insert(location, idx);
213210

214-
// This assert is a good sanity check until more general 2-phase borrow
215-
// support is introduced. See NOTE on the activation_map field for more
216-
assert!(!self.activation_map.contains_key(&activate_location),
217-
"More than one activation introduced at the same location.");
218-
self.activation_map.insert(activate_location, idx);
219-
211+
insert(&mut self.activation_map, &activate_location, idx);
220212
insert(&mut self.assigned_map, assigned_place, idx);
221213
insert(&mut self.region_map, &region, idx);
222214
if let Some(local) = root_local(borrowed_place) {
@@ -552,9 +544,11 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
552544
location: Location) {
553545
// Handle activations
554546
match self.activation_map.get(&location) {
555-
Some(&activated) => {
556-
debug!("activating borrow {:?}", activated);
557-
sets.gen(&ReserveOrActivateIndex::active(activated))
547+
Some(activations) => {
548+
for activated in activations {
549+
debug!("activating borrow {:?}", activated);
550+
sets.gen(&ReserveOrActivateIndex::active(*activated))
551+
}
558552
}
559553
None => {}
560554
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2018 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+
#![feature(nll)]
12+
13+
struct Foo {
14+
}
15+
16+
impl Foo {
17+
fn method(&mut self, foo: &mut Foo) {
18+
}
19+
}
20+
21+
fn main() {
22+
let mut foo = Foo { };
23+
foo.method(&mut foo);
24+
//~^ cannot borrow `foo` as mutable more than once at a time
25+
//~^^ cannot borrow `foo` as mutable more than once at a time
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0499]: cannot borrow `foo` as mutable more than once at a time
2+
--> $DIR/two-phase-multi-mut.rs:23:16
3+
|
4+
LL | foo.method(&mut foo);
5+
| -----------^^^^^^^^-
6+
| | |
7+
| | second mutable borrow occurs here
8+
| first mutable borrow occurs here
9+
| borrow later used here
10+
11+
error[E0499]: cannot borrow `foo` as mutable more than once at a time
12+
--> $DIR/two-phase-multi-mut.rs:23:5
13+
|
14+
LL | foo.method(&mut foo);
15+
| ^^^^^^^^^^^--------^
16+
| | |
17+
| | first mutable borrow occurs here
18+
| second mutable borrow occurs here
19+
| borrow later used here
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0499`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 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+
// run-pass
16+
17+
use std::io::Result;
18+
19+
struct Foo {}
20+
21+
pub trait FakeRead {
22+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>;
23+
}
24+
25+
impl FakeRead for Foo {
26+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
27+
Ok(4)
28+
}
29+
}
30+
31+
fn main() {
32+
let mut a = Foo {};
33+
let mut v = Vec::new();
34+
a.read_to_end(&mut v);
35+
}

0 commit comments

Comments
 (0)