Skip to content

Commit 2247cd6

Browse files
committed
Simplify visit_statement.
1 parent 9a56933 commit 2247cd6

File tree

2 files changed

+61
-65
lines changed

2 files changed

+61
-65
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,14 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
961961
}
962962
}
963963
}
964-
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
964+
StatementKind::StorageLive(local) => {
965965
let frame = self.ecx.frame_mut();
966-
frame.locals[local].value = if let StatementKind::StorageLive(_) = statement.kind {
967-
LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit))
968-
} else {
969-
LocalValue::Dead
970-
};
966+
frame.locals[local].value =
967+
LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit));
968+
}
969+
StatementKind::StorageDead(local) => {
970+
let frame = self.ecx.frame_mut();
971+
frame.locals[local].value = LocalValue::Dead;
971972
}
972973
_ => {}
973974
}

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -522,75 +522,70 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
522522
trace!("visit_statement: {:?}", statement);
523523
let source_info = statement.source_info;
524524
self.source_info = Some(source_info);
525-
if let StatementKind::Assign(box (place, ref rval)) = statement.kind {
526-
let can_const_prop = self.ecx.machine.can_const_prop[place.local];
527-
if let Some(()) = self.const_prop(rval, source_info, place) {
528-
match can_const_prop {
529-
ConstPropMode::OnlyInsideOwnBlock => {
530-
trace!(
531-
"found local restricted to its block. \
525+
match statement.kind {
526+
StatementKind::Assign(box (place, ref rval)) => {
527+
let can_const_prop = self.ecx.machine.can_const_prop[place.local];
528+
if let Some(()) = self.const_prop(rval, source_info, place) {
529+
match can_const_prop {
530+
ConstPropMode::OnlyInsideOwnBlock => {
531+
trace!(
532+
"found local restricted to its block. \
532533
Will remove it from const-prop after block is finished. Local: {:?}",
533-
place.local
534-
);
535-
}
536-
ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
537-
trace!("can't propagate into {:?}", place);
538-
if place.local != RETURN_PLACE {
539-
Self::remove_const(&mut self.ecx, place.local);
534+
place.local
535+
);
540536
}
541-
}
542-
ConstPropMode::FullConstProp => {}
543-
}
544-
} else {
545-
// Const prop failed, so erase the destination, ensuring that whatever happens
546-
// from here on, does not know about the previous value.
547-
// This is important in case we have
548-
// ```rust
549-
// let mut x = 42;
550-
// x = SOME_MUTABLE_STATIC;
551-
// // x must now be uninit
552-
// ```
553-
// FIXME: we overzealously erase the entire local, because that's easier to
554-
// implement.
555-
trace!(
556-
"propagation into {:?} failed.
557-
Nuking the entire site from orbit, it's the only way to be sure",
558-
place,
559-
);
560-
Self::remove_const(&mut self.ecx, place.local);
561-
}
562-
} else {
563-
match statement.kind {
564-
StatementKind::SetDiscriminant { ref place, .. } => {
565-
match self.ecx.machine.can_const_prop[place.local] {
566-
ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
567-
if self
568-
.use_ecx(source_info, |this| this.ecx.statement(statement))
569-
.is_some()
570-
{
571-
trace!("propped discriminant into {:?}", place);
572-
} else {
537+
ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
538+
trace!("can't propagate into {:?}", place);
539+
if place.local != RETURN_PLACE {
573540
Self::remove_const(&mut self.ecx, place.local);
574541
}
575542
}
576-
ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
577-
Self::remove_const(&mut self.ecx, place.local);
578-
}
543+
ConstPropMode::FullConstProp => {}
579544
}
545+
} else {
546+
// Const prop failed, so erase the destination, ensuring that whatever happens
547+
// from here on, does not know about the previous value.
548+
// This is important in case we have
549+
// ```rust
550+
// let mut x = 42;
551+
// x = SOME_MUTABLE_STATIC;
552+
// // x must now be uninit
553+
// ```
554+
// FIXME: we overzealously erase the entire local, because that's easier to
555+
// implement.
556+
trace!(
557+
"propagation into {:?} failed.
558+
Nuking the entire site from orbit, it's the only way to be sure",
559+
place,
560+
);
561+
Self::remove_const(&mut self.ecx, place.local);
580562
}
581-
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
582-
let frame = self.ecx.frame_mut();
583-
frame.locals[local].value =
584-
if let StatementKind::StorageLive(_) = statement.kind {
585-
LocalValue::Live(interpret::Operand::Immediate(
586-
interpret::Immediate::Uninit,
587-
))
563+
}
564+
StatementKind::SetDiscriminant { ref place, .. } => {
565+
match self.ecx.machine.can_const_prop[place.local] {
566+
ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
567+
if self.use_ecx(source_info, |this| this.ecx.statement(statement)).is_some()
568+
{
569+
trace!("propped discriminant into {:?}", place);
588570
} else {
589-
LocalValue::Dead
590-
};
571+
Self::remove_const(&mut self.ecx, place.local);
572+
}
573+
}
574+
ConstPropMode::OnlyPropagateInto | ConstPropMode::NoPropagation => {
575+
Self::remove_const(&mut self.ecx, place.local);
576+
}
591577
}
592-
_ => {}
593578
}
579+
StatementKind::StorageLive(local) => {
580+
let frame = self.ecx.frame_mut();
581+
frame.locals[local].value =
582+
LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit));
583+
}
584+
StatementKind::StorageDead(local) => {
585+
let frame = self.ecx.frame_mut();
586+
frame.locals[local].value = LocalValue::Dead;
587+
}
588+
_ => {}
594589
}
595590

596591
self.super_statement(statement, location);

0 commit comments

Comments
 (0)