@@ -706,12 +706,22 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
706
706
let helper = |rhs : & Rvalue < ' tcx > ,
707
707
place : & Place < ' tcx > ,
708
708
variant_index : & VariantIdx ,
709
+ switch_value : u128 ,
709
710
side_to_choose| {
710
711
let place_type = place. ty ( self . body , self . tcx ) . ty ;
711
712
let adt = match * place_type. kind ( ) {
712
713
ty:: Adt ( adt, _) if adt. is_enum ( ) => adt,
713
714
_ => return StatementEquality :: NotEqual ,
714
715
} ;
716
+ let variant_discr = adt. discriminant_for_variant ( self . tcx , * variant_index) . val ;
717
+ if variant_discr != switch_value {
718
+ trace ! (
719
+ "NO: variant discriminant {} does not equal switch value {}" ,
720
+ variant_discr,
721
+ switch_value
722
+ ) ;
723
+ return StatementEquality :: NotEqual ;
724
+ }
715
725
let variant_is_fieldless = adt. variants [ * variant_index] . fields . is_empty ( ) ;
716
726
if !variant_is_fieldless {
717
727
trace ! ( "NO: variant {:?} was not fieldless" , variant_index) ;
@@ -742,18 +752,18 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
742
752
StatementKind :: SetDiscriminant { place, variant_index } ,
743
753
)
744
754
// we need to make sure that the switch value that targets the bb with SetDiscriminant (y), is the same as the variant index
745
- if Some ( variant_index . index ( ) as u128 ) == y_target_and_value. value => {
755
+ if y_target_and_value. value . is_some ( ) => {
746
756
// choose basic block of x, as that has the assign
747
- helper ( rhs, place, variant_index, x_target_and_value. target )
757
+ helper ( rhs, place, variant_index, y_target_and_value . value . unwrap ( ) , x_target_and_value. target )
748
758
}
749
759
(
750
760
StatementKind :: SetDiscriminant { place, variant_index } ,
751
761
StatementKind :: Assign ( box ( _, rhs) ) ,
752
762
)
753
763
// we need to make sure that the switch value that targets the bb with SetDiscriminant (x), is the same as the variant index
754
- if Some ( variant_index . index ( ) as u128 ) == x_target_and_value. value => {
764
+ if x_target_and_value. value . is_some ( ) => {
755
765
// choose basic block of y, as that has the assign
756
- helper ( rhs, place, variant_index, y_target_and_value. target )
766
+ helper ( rhs, place, variant_index, x_target_and_value . value . unwrap ( ) , y_target_and_value. target )
757
767
}
758
768
_ => {
759
769
trace ! ( "NO: statements `{:?}` and `{:?}` not considered equal" , x, y) ;
0 commit comments