Skip to content

Commit b862b43

Browse files
committed
Auto merge of #96888 - Aaron1011:fake-borrow-no-sort, r=petrochenkov
Use `FxIndexSet` to avoid sorting fake borrows This fixes #96449, but I haven't yet been able to make the reproducer work using `#[cfg]` attributes, so we can't use the 'revision' infra to write a test The previous implementation relied on sorting by `PlaceRef`. This requires sorting by a `DefId`, which uses untracked state (see #93315)
2 parents 532be94 + aa0cc9c commit b862b43

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
1111
use crate::build::{BlockAnd, BlockAndExtension, Builder};
1212
use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
1313
use rustc_data_structures::{
14-
fx::{FxHashSet, FxIndexMap},
14+
fx::{FxIndexMap, FxIndexSet},
1515
stack::ensure_sufficient_stack,
1616
};
1717
use rustc_hir::HirId;
@@ -264,7 +264,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
264264
// The set of places that we are creating fake borrows of. If there are
265265
// no match guards then we don't need any fake borrows, so don't track
266266
// them.
267-
let mut fake_borrows = match_has_guard.then(FxHashSet::default);
267+
let mut fake_borrows = match_has_guard.then(FxIndexSet::default);
268268

269269
let mut otherwise = None;
270270

@@ -1053,7 +1053,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10531053
start_block: BasicBlock,
10541054
otherwise_block: &mut Option<BasicBlock>,
10551055
candidates: &mut [&mut Candidate<'pat, 'tcx>],
1056-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1056+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
10571057
) {
10581058
debug!(
10591059
"matched_candidate(span={:?}, candidates={:?}, start_block={:?}, otherwise_block={:?})",
@@ -1105,7 +1105,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11051105
start_block: BasicBlock,
11061106
otherwise_block: &mut Option<BasicBlock>,
11071107
candidates: &mut [&mut Candidate<'_, 'tcx>],
1108-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1108+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
11091109
) {
11101110
// The candidates are sorted by priority. Check to see whether the
11111111
// higher priority candidates (and hence at the front of the slice)
@@ -1184,7 +1184,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11841184
&mut self,
11851185
matched_candidates: &mut [&mut Candidate<'_, 'tcx>],
11861186
start_block: BasicBlock,
1187-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1187+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
11881188
) -> Option<BasicBlock> {
11891189
debug_assert!(
11901190
!matched_candidates.is_empty(),
@@ -1322,7 +1322,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13221322
candidates: &mut [&mut Candidate<'_, 'tcx>],
13231323
block: BasicBlock,
13241324
otherwise_block: &mut Option<BasicBlock>,
1325-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1325+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
13261326
) {
13271327
let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap();
13281328

@@ -1385,7 +1385,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13851385
pats: &'pat [Pat<'tcx>],
13861386
or_span: Span,
13871387
place: PlaceBuilder<'tcx>,
1388-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1388+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
13891389
) {
13901390
debug!("test_or_pattern:\ncandidate={:#?}\npats={:#?}", candidate, pats);
13911391
let mut or_candidates: Vec<_> = pats
@@ -1572,7 +1572,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15721572
mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
15731573
block: BasicBlock,
15741574
otherwise_block: &mut Option<BasicBlock>,
1575-
fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1575+
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
15761576
) {
15771577
// extract the match-pair from the highest priority candidate
15781578
let match_pair = &candidates.first().unwrap().match_pairs[0];
@@ -1715,7 +1715,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17151715
/// by a MIR pass run after borrow checking.
17161716
fn calculate_fake_borrows<'b>(
17171717
&mut self,
1718-
fake_borrows: &'b FxHashSet<Place<'tcx>>,
1718+
fake_borrows: &'b FxIndexSet<Place<'tcx>>,
17191719
temp_span: Span,
17201720
) -> Vec<(Place<'tcx>, Local)> {
17211721
let tcx = self.tcx;
@@ -1741,8 +1741,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17411741
all_fake_borrows.push(place.as_ref());
17421742
}
17431743

1744-
// Deduplicate and ensure a deterministic order.
1745-
all_fake_borrows.sort();
17461744
all_fake_borrows.dedup();
17471745

17481746
debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);

src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
let mut _0: i32; // return place in scope 0 at $DIR/remove_fake_borrows.rs:6:46: 6:49
88
let mut _3: isize; // in scope 0 at $DIR/remove_fake_borrows.rs:8:9: 8:16
99
let mut _4: &std::option::Option<&&i32>; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
10-
let mut _5: &&&i32; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
11-
let mut _6: &&i32; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
10+
let mut _5: &&i32; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
11+
let mut _6: &&&i32; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
1212
let mut _7: &i32; // in scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
1313
let mut _8: bool; // in scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
1414

@@ -34,8 +34,8 @@
3434

3535
bb4: {
3636
- _4 = &shallow _1; // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
37-
- _5 = &shallow ((_1 as Some).0: &&i32); // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
38-
- _6 = &shallow (*((_1 as Some).0: &&i32)); // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
37+
- _5 = &shallow (*((_1 as Some).0: &&i32)); // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
38+
- _6 = &shallow ((_1 as Some).0: &&i32); // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
3939
- _7 = &shallow (*(*((_1 as Some).0: &&i32))); // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
4040
+ nop; // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12
4141
+ nop; // scope 0 at $DIR/remove_fake_borrows.rs:7:11: 7:12

0 commit comments

Comments
 (0)