Skip to content

Commit df81dee

Browse files
committed
Ignore some input for compile-time
1 parent 5b984a0 commit df81dee

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ fn simplify_to_copy<'tcx>(
548548
switch_bb_idx: BasicBlock,
549549
param_env: ParamEnv<'tcx>,
550550
) -> Option<()> {
551+
if switch_bb_idx != START_BLOCK {
552+
return None;
553+
}
551554
let bbs = &body.basic_blocks;
552555
// Check if the copy source matches the following pattern.
553556
// _2 = discriminant(*_1); // "*_1" is the expected the copy source.
@@ -563,6 +566,11 @@ fn simplify_to_copy<'tcx>(
563566
if !expected_src_ty.ty.is_enum() || expected_src_ty.variant_index.is_some() {
564567
return None;
565568
}
569+
let expected_dest_place = Place::return_place();
570+
let expected_dest_ty = expected_dest_place.ty(body.local_decls(), tcx);
571+
if expected_dest_ty.ty != expected_src_ty.ty || expected_dest_ty.variant_index.is_some() {
572+
return None;
573+
}
566574
let targets = match bbs[switch_bb_idx].terminator().kind {
567575
TerminatorKind::SwitchInt { ref discr, ref targets, .. }
568576
if discr.place() == Some(discr_place) =>
@@ -589,7 +597,7 @@ fn simplify_to_copy<'tcx>(
589597

590598
let borrowed_locals = borrowed_locals(body);
591599
let mut live = None;
592-
let mut expected_dest_place = None;
600+
593601
for (index, target_bb) in targets.iter() {
594602
let stmts = &bbs[target_bb].statements;
595603
if stmts.is_empty() {
@@ -605,7 +613,7 @@ fn simplify_to_copy<'tcx>(
605613
let ty::Adt(def, _) = dest_ty.ty.kind() else {
606614
return None;
607615
};
608-
if *expected_dest_place.get_or_insert(*place) != *place {
616+
if expected_dest_place != *place {
609617
return None;
610618
}
611619
match rvalue {
@@ -637,7 +645,7 @@ fn simplify_to_copy<'tcx>(
637645
// If the BB contains more than one statement, we have to check if these statements can be ignored.
638646
let mut lived_stmts: BitSet<usize> =
639647
BitSet::new_filled(bbs[target_bb].statements.len());
640-
let mut dest_place = None;
648+
let mut expected_copy_stmt = None;
641649
for (statement_index, statement) in bbs[target_bb].statements.iter().enumerate().rev() {
642650
let loc = Location { block: target_bb, statement_index };
643651
if let StatementKind::Assign(assign) = &statement.kind {
@@ -661,13 +669,16 @@ fn simplify_to_copy<'tcx>(
661669
live.seek_before_primary_effect(loc);
662670
if !live.get().contains(place.local) {
663671
lived_stmts.remove(statement_index);
664-
} else if matches!(
665-
&statement.kind,
666-
StatementKind::Assign(box (_, Rvalue::Use(Operand::Copy(_))))
667-
) && dest_place.is_none()
672+
} else if let StatementKind::Assign(box (
673+
_,
674+
Rvalue::Use(Operand::Copy(src_place)),
675+
)) = statement.kind
676+
&& expected_copy_stmt.is_none()
677+
&& expected_src_place == src_place
678+
&& expected_dest_place == *place
668679
{
669680
// There is only one statement that cannot be ignored that can be used as an expected copy statement.
670-
dest_place = Some(*place);
681+
expected_copy_stmt = Some(statement_index);
671682
} else {
672683
return None;
673684
}
@@ -687,21 +698,19 @@ fn simplify_to_copy<'tcx>(
687698
}
688699
}
689700
}
690-
let dest_place = dest_place?;
691-
if *expected_dest_place.get_or_insert(dest_place) != dest_place {
692-
return None;
693-
}
701+
let expected_copy_stmt = expected_copy_stmt?;
694702
// We can ignore the paired StorageLive and StorageDead.
695703
let mut storage_live_locals: BitSet<Local> = BitSet::new_empty(body.local_decls.len());
696704
for stmt_index in lived_stmts.iter() {
697705
let statement = &bbs[target_bb].statements[stmt_index];
698706
match &statement.kind {
699-
StatementKind::Assign(box (place, Rvalue::Use(Operand::Copy(src_place))))
700-
if *place == dest_place && *src_place == expected_src_place => {}
707+
StatementKind::Assign(_) if expected_copy_stmt == stmt_index => {}
701708
StatementKind::StorageLive(local)
702-
if *local != dest_place.local && storage_live_locals.insert(*local) => {}
709+
if *local != expected_dest_place.local
710+
&& storage_live_locals.insert(*local) => {}
703711
StatementKind::StorageDead(local)
704-
if *local != dest_place.local && storage_live_locals.remove(*local) => {}
712+
if *local != expected_dest_place.local
713+
&& storage_live_locals.remove(*local) => {}
705714
StatementKind::Nop => {}
706715
_ => return None,
707716
}
@@ -711,7 +720,6 @@ fn simplify_to_copy<'tcx>(
711720
}
712721
}
713722
}
714-
let expected_dest_place = expected_dest_place?;
715723
let statement_index = bbs[switch_bb_idx].statements.len();
716724
let parent_end = Location { block: switch_bb_idx, statement_index };
717725
let mut patch = MirPatch::new(body);

0 commit comments

Comments
 (0)