@@ -240,30 +240,14 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
240
240
if is_switch ( terminator) {
241
241
let this_bb_discr_info = self . find_switch_discriminant_info ( bb, terminator) ?;
242
242
243
- // The layouts of the two ADTs have to be equal for this optimization to apply
244
- let layout_of_adt =
245
- |ty : Ty < ' tcx > | self . tcx . layout_of ( self . param_env . and ( ty) ) . ok ( ) . map ( |x| x. layout ) ;
246
- let layout_lhs = layout_of_adt ( discr_info. type_adt_matched_on ) ;
247
- let layout_rhs = layout_of_adt ( this_bb_discr_info. type_adt_matched_on ) ;
248
- if layout_lhs != layout_rhs {
249
- trace ! ( "NO: Layouts do not match. LHS: {:?}, RHS: {:?}" , layout_lhs, layout_rhs) ;
250
- return None ;
251
- }
252
-
253
- // the otherwise branch of the two switches have to point to the same bb
243
+ // The otherwise branch of the two switches have to point to the same bb
254
244
if discr_info. otherwise_bb != this_bb_discr_info. otherwise_bb {
255
245
trace ! ( "NO: otherwise target is not the same" ) ;
256
246
return None ;
257
247
}
258
248
259
- // check that the value being matched on is the same. The
260
- if this_bb_discr_info. targets_with_values . iter ( ) . find ( |x| x. 1 == value) . is_none ( ) {
261
- trace ! ( "NO: values being matched on are not the same" ) ;
262
- return None ;
263
- }
264
-
265
- // only allow optimization if the left and right of the tuple being matched are the same variants.
266
- // so the following should not optimize
249
+ // Only allow optimization if the left and right of the tuple being matched
250
+ // are the same variants. So the following should not optimize
267
251
// ```rust
268
252
// let x: Option<()>;
269
253
// let y: Option<()>;
@@ -272,7 +256,8 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
272
256
// _ => {}
273
257
// }
274
258
// ```
275
- // We check this by seeing that the value of the first discriminant is the only other discriminant value being used as a target in the second switch
259
+ // We check this by seeing that the value of the first discriminant is the only
260
+ // other discriminant value being used as a target in the second switch
276
261
if !( this_bb_discr_info. targets_with_values . len ( ) == 1
277
262
&& this_bb_discr_info. targets_with_values [ 0 ] . 1 == value)
278
263
{
@@ -282,7 +267,23 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
282
267
return None ;
283
268
}
284
269
285
- // if we reach this point, the optimization applies, and we should be able to optimize this case
270
+ // check that the value being matched on is the same. The
271
+ if this_bb_discr_info. targets_with_values . iter ( ) . find ( |x| x. 1 == value) . is_none ( ) {
272
+ trace ! ( "NO: values being matched on are not the same" ) ;
273
+ return None ;
274
+ }
275
+
276
+ // The layouts of the two ADTs have to be equal for this optimization to apply
277
+ let layout_of_adt =
278
+ |ty : Ty < ' tcx > | self . tcx . layout_of ( self . param_env . and ( ty) ) . ok ( ) . map ( |x| x. layout ) ;
279
+ let layout_lhs = layout_of_adt ( discr_info. type_adt_matched_on ) ;
280
+ let layout_rhs = layout_of_adt ( this_bb_discr_info. type_adt_matched_on ) ;
281
+ if layout_lhs != layout_rhs {
282
+ trace ! ( "NO: Layouts do not match. LHS: {:?}, RHS: {:?}" , layout_lhs, layout_rhs) ;
283
+ return None ;
284
+ }
285
+
286
+ // If we reach this point, the optimization applies, and we should be able to optimize this case
286
287
// store the info that is needed to apply the optimization
287
288
288
289
Some ( OptimizationInfo {
0 commit comments