Skip to content

Commit e561dbb

Browse files
committed
Move everything into the loop
1 parent 0896a49 commit e561dbb

File tree

1 file changed

+36
-38
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+36
-38
lines changed

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

+36-38
Original file line numberDiff line numberDiff line change
@@ -1272,48 +1272,46 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12721272
start_block: BasicBlock,
12731273
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
12741274
) -> BasicBlock {
1275-
debug_assert!(
1276-
matched_candidates.iter().all(|c| c.subcandidates.is_empty()),
1277-
"subcandidates should be empty in select_matched_candidates",
1278-
);
1279-
1280-
// Insert a borrows of prefixes of places that are bound and are
1281-
// behind a dereference projection.
1282-
//
1283-
// These borrows are taken to avoid situations like the following:
1284-
//
1285-
// match x[10] {
1286-
// _ if { x = &[0]; false } => (),
1287-
// y => (), // Out of bounds array access!
1288-
// }
1289-
//
1290-
// match *x {
1291-
// // y is bound by reference in the guard and then by copy in the
1292-
// // arm, so y is 2 in the arm!
1293-
// y if { y == 1 && (x = &2) == () } => y,
1294-
// _ => 3,
1295-
// }
1296-
if let Some(fake_borrows) = fake_borrows {
1297-
for Binding { source, .. } in
1298-
matched_candidates.iter().flat_map(|candidate| &candidate.bindings)
1299-
{
1300-
if let Some(i) =
1301-
source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref)
1302-
{
1303-
let proj_base = &source.projection[..i];
1304-
1305-
fake_borrows.insert(Place {
1306-
local: source.local,
1307-
projection: self.tcx.mk_place_elems(proj_base),
1308-
});
1309-
}
1310-
}
1311-
}
1312-
13131275
let mut next_prebinding = start_block;
13141276
for candidate in matched_candidates.iter_mut() {
13151277
assert!(candidate.otherwise_block.is_none());
13161278
assert!(candidate.pre_binding_block.is_none());
1279+
debug_assert!(
1280+
candidate.subcandidates.is_empty(),
1281+
"subcandidates should be empty in select_matched_candidates",
1282+
);
1283+
1284+
if let Some(fake_borrows) = fake_borrows {
1285+
// Insert a borrows of prefixes of places that are bound and are
1286+
// behind a dereference projection.
1287+
//
1288+
// These borrows are taken to avoid situations like the following:
1289+
//
1290+
// match x[10] {
1291+
// _ if { x = &[0]; false } => (),
1292+
// y => (), // Out of bounds array access!
1293+
// }
1294+
//
1295+
// match *x {
1296+
// // y is bound by reference in the guard and then by copy in the
1297+
// // arm, so y is 2 in the arm!
1298+
// y if { y == 1 && (x = &2) == () } => y,
1299+
// _ => 3,
1300+
// }
1301+
for Binding { source, .. } in &candidate.bindings {
1302+
if let Some(i) =
1303+
source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref)
1304+
{
1305+
let proj_base = &source.projection[..i];
1306+
1307+
fake_borrows.insert(Place {
1308+
local: source.local,
1309+
projection: self.tcx.mk_place_elems(proj_base),
1310+
});
1311+
}
1312+
}
1313+
}
1314+
13171315
candidate.pre_binding_block = Some(next_prebinding);
13181316
next_prebinding = self.cfg.start_new_block();
13191317
if candidate.has_guard {

0 commit comments

Comments
 (0)