Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsound optimization with explicit variant discriminants #89489

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions compiler/rustc_mir_transform/src/simplify_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
ty::Adt(adt, _) if adt.is_enum() => adt,
_ => return StatementEquality::NotEqual,
};
// We need to make sure that the switch value that targets the bb with
// SetDiscriminant is the same as the variant discriminant.
let variant_discr = adt.discriminant_for_variant(self.tcx, *variant_index).val;
if variant_discr != switch_value {
trace!(
Expand Down Expand Up @@ -750,20 +752,28 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
(
StatementKind::Assign(box (_, rhs)),
StatementKind::SetDiscriminant { place, variant_index },
)
// we need to make sure that the switch value that targets the bb with SetDiscriminant (y), is the same as the variant index
if y_target_and_value.value.is_some() => {
) if y_target_and_value.value.is_some() => {
// choose basic block of x, as that has the assign
helper(rhs, place, variant_index, y_target_and_value.value.unwrap(), x_target_and_value.target)
helper(
rhs,
place,
variant_index,
y_target_and_value.value.unwrap(),
x_target_and_value.target,
)
}
(
StatementKind::SetDiscriminant { place, variant_index },
StatementKind::Assign(box (_, rhs)),
)
// we need to make sure that the switch value that targets the bb with SetDiscriminant (x), is the same as the variant index
if x_target_and_value.value.is_some() => {
) if x_target_and_value.value.is_some() => {
// choose basic block of y, as that has the assign
helper(rhs, place, variant_index, x_target_and_value.value.unwrap(), y_target_and_value.target)
helper(
rhs,
place,
variant_index,
x_target_and_value.value.unwrap(),
y_target_and_value.target,
)
camelid marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {
trace!("NO: statements `{:?}` and `{:?}` not considered equal", x, y);
Expand Down