Skip to content

Commit 17d4e9b

Browse files
committed
Make killing of out-of-scope borrows a pre-statement effect
Fixes #46875. Fixes #46917. Fixes #46935.
1 parent 063b998 commit 17d4e9b

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

Diff for: src/librustc_mir/borrow_check/flows.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
8888
};
8989
saw_one = true;
9090
let borrow_data = &self.borrows.operator().borrows()[borrow.borrow_index()];
91-
s.push_str(&format!("{}", borrow_data));
91+
s.push_str(&format!("{}{}", borrow_data,
92+
if borrow.is_activation() { "@active" } else { "" }));
9293
});
9394
s.push_str("] ");
9495

Diff for: src/librustc_mir/borrow_check/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
20112011
let borrowed = &data[i.borrow_index()];
20122012

20132013
if self.places_conflict(&borrowed.borrowed_place, place, access) {
2014+
debug!("each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
2015+
i, borrowed, place, access);
20142016
let ctrl = op(self, i, borrowed);
20152017
if ctrl == Control::Break {
20162018
return;

Diff for: src/librustc_mir/dataflow/impls/borrows.rs

+28
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,27 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Reservations<'a, 'gcx, 'tcx> {
635635
// `_sets`.
636636
}
637637

638+
fn before_statement_effect(&self,
639+
sets: &mut BlockSets<ReserveOrActivateIndex>,
640+
location: Location) {
641+
debug!("Reservations::before_statement_effect sets: {:?} location: {:?}", sets, location);
642+
self.0.kill_loans_out_of_scope_at_location(sets, location, false);
643+
}
644+
638645
fn statement_effect(&self,
639646
sets: &mut BlockSets<ReserveOrActivateIndex>,
640647
location: Location) {
641648
debug!("Reservations::statement_effect sets: {:?} location: {:?}", sets, location);
642649
self.0.statement_effect_on_borrows(sets, location, false);
643650
}
644651

652+
fn before_terminator_effect(&self,
653+
sets: &mut BlockSets<ReserveOrActivateIndex>,
654+
location: Location) {
655+
debug!("Reservations::before_terminator_effect sets: {:?} location: {:?}", sets, location);
656+
self.0.kill_loans_out_of_scope_at_location(sets, location, false);
657+
}
658+
645659
fn terminator_effect(&self,
646660
sets: &mut BlockSets<ReserveOrActivateIndex>,
647661
location: Location) {
@@ -682,13 +696,27 @@ impl<'a, 'gcx, 'tcx> BitDenotation for ActiveBorrows<'a, 'gcx, 'tcx> {
682696
// `_sets`.
683697
}
684698

699+
fn before_statement_effect(&self,
700+
sets: &mut BlockSets<ReserveOrActivateIndex>,
701+
location: Location) {
702+
debug!("ActiveBorrows::before_statement_effect sets: {:?} location: {:?}", sets, location);
703+
self.0.kill_loans_out_of_scope_at_location(sets, location, true);
704+
}
705+
685706
fn statement_effect(&self,
686707
sets: &mut BlockSets<ReserveOrActivateIndex>,
687708
location: Location) {
688709
debug!("ActiveBorrows::statement_effect sets: {:?} location: {:?}", sets, location);
689710
self.0.statement_effect_on_borrows(sets, location, true);
690711
}
691712

713+
fn before_terminator_effect(&self,
714+
sets: &mut BlockSets<ReserveOrActivateIndex>,
715+
location: Location) {
716+
debug!("ActiveBorrows::before_terminator_effect sets: {:?} location: {:?}", sets, location);
717+
self.0.kill_loans_out_of_scope_at_location(sets, location, true);
718+
}
719+
692720
fn terminator_effect(&self,
693721
sets: &mut BlockSets<ReserveOrActivateIndex>,
694722
location: Location) {

Diff for: src/test/compile-fail/borrowck/two-phase-activation-sharing-interference.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ fn should_also_eventually_be_ok_with_nll() {
5656
let _z = &x;
5757
*y += 1;
5858
//[lxl]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
59-
//[nll]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
6059
}
6160

6261
fn main() { }

Diff for: src/test/ui/nll/borrow-use-issue-46875.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#![feature(nll)]
12+
13+
// run-pass
14+
15+
fn vec() {
16+
let mut _x = vec!['c'];
17+
let _y = &_x;
18+
_x = Vec::new();
19+
}
20+
21+
fn int() {
22+
let mut _x = 5;
23+
let _y = &_x;
24+
_x = 7;
25+
}
26+
27+
fn main() {
28+
vec();
29+
int();
30+
}

0 commit comments

Comments
 (0)