Skip to content

Commit 87a9d98

Browse files
committed
Only consider places with the same local in each_borrow_involving_path.
1 parent fdd0301 commit 87a9d98

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

compiler/rustc_borrowck/src/invalidation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,14 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
351351
let tcx = self.tcx;
352352
let body = self.body;
353353
let borrow_set = self.borrow_set;
354-
let indices = self.borrow_set.indices();
355354
each_borrow_involving_path(
356355
self,
357356
tcx,
358357
body,
359358
location,
360359
(sd, place),
361360
borrow_set,
362-
indices,
361+
|_| true,
363362
|this, borrow_index, borrow| {
364363
match (rw, borrow.kind) {
365364
// Obviously an activation is compatible with its own

compiler/rustc_borrowck/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticMessage, Subdiagnost
2323
use rustc_fluent_macro::fluent_messages;
2424
use rustc_hir as hir;
2525
use rustc_hir::def_id::LocalDefId;
26-
use rustc_index::bit_set::ChunkedBitSet;
26+
use rustc_index::bit_set::{BitSet, ChunkedBitSet};
2727
use rustc_index::{IndexSlice, IndexVec};
2828
use rustc_infer::infer::{
2929
DefiningAnchor, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
@@ -41,7 +41,6 @@ use rustc_session::lint::builtin::UNUSED_MUT;
4141
use rustc_span::{Span, Symbol};
4242
use rustc_target::abi::FieldIdx;
4343

44-
use either::Either;
4544
use smallvec::SmallVec;
4645
use std::cell::OnceCell;
4746
use std::cell::RefCell;
@@ -1049,12 +1048,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10491048
let borrow_set = self.borrow_set.clone();
10501049

10511050
// Use polonius output if it has been enabled.
1052-
let polonius_output = self.polonius_output.clone();
1053-
let borrows_in_scope = if let Some(polonius) = &polonius_output {
1051+
let mut polonius_output;
1052+
let borrows_in_scope = if let Some(polonius) = &self.polonius_output {
10541053
let location = self.location_table.start_index(location);
1055-
Either::Left(polonius.errors_at(location).iter().copied())
1054+
polonius_output = BitSet::new_empty(borrow_set.len());
1055+
for &idx in polonius.errors_at(location) {
1056+
polonius_output.insert(idx);
1057+
}
1058+
&polonius_output
10561059
} else {
1057-
Either::Right(flow_state.borrows.iter())
1060+
&flow_state.borrows
10581061
};
10591062

10601063
each_borrow_involving_path(
@@ -1064,7 +1067,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10641067
location,
10651068
(sd, place_span.0),
10661069
&borrow_set,
1067-
borrows_in_scope,
1070+
|borrow_index| borrows_in_scope.contains(borrow_index),
10681071
|this, borrow_index, borrow| match (rw, borrow.kind) {
10691072
// Obviously an activation is compatible with its own
10701073
// reservation (or even prior activating uses of same

compiler/rustc_borrowck/src/path_utils.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ pub(super) fn each_borrow_involving_path<'tcx, F, I, S>(
3333
_location: Location,
3434
access_place: (AccessDepth, Place<'tcx>),
3535
borrow_set: &BorrowSet<'tcx>,
36-
candidates: I,
36+
is_candidate: I,
3737
mut op: F,
3838
) where
3939
F: FnMut(&mut S, BorrowIndex, &BorrowData<'tcx>) -> Control,
40-
I: Iterator<Item = BorrowIndex>,
40+
I: Fn(BorrowIndex) -> bool,
4141
{
4242
let (access, place) = access_place;
4343

44-
// FIXME: analogous code in check_loans first maps `place` to
45-
// its base_path.
44+
// The number of candidates can be large, but borrows for different locals cannot conflict with
45+
// each other, so we restrict the working set a priori.
46+
let Some(borrows_for_place_base) = borrow_set.local_map.get(&place.local) else { return };
4647

4748
// check for loan restricting path P being used. Accounts for
4849
// borrows of P, P.a.b, etc.
49-
for i in candidates {
50+
for &i in borrows_for_place_base {
51+
if !is_candidate(i) {
52+
continue;
53+
}
5054
let borrowed = &borrow_set[i];
5155

5256
if places_conflict::borrow_conflicts_with_place(

0 commit comments

Comments
 (0)