Skip to content

Commit be6c364

Browse files
committed
simplify_match_pair: avoid the second comparison if possible.
Also, the `try_to_bits` always succeeds, so use `unwrap`.
1 parent c4cd044 commit be6c364

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

compiler/rustc_mir_build/src/build/matches/simplify.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
227227
_ => (None, 0),
228228
};
229229
if let Some((min, max, sz)) = range {
230-
if let (Some(lo), Some(hi)) = (lo.try_to_bits(sz), hi.try_to_bits(sz)) {
231-
// We want to compare ranges numerically, but the order of the bitwise
232-
// representation of signed integers does not match their numeric order.
233-
// Thus, to correct the ordering, we need to shift the range of signed
234-
// integers to correct the comparison. This is achieved by XORing with a
235-
// bias (see pattern/_match.rs for another pertinent example of this
236-
// pattern).
237-
let (lo, hi) = (lo ^ bias, hi ^ bias);
238-
if lo <= min && (hi > max || hi == max && end == RangeEnd::Included) {
230+
// We want to compare ranges numerically, but the order of the bitwise
231+
// representation of signed integers does not match their numeric order. Thus,
232+
// to correct the ordering, we need to shift the range of signed integers to
233+
// correct the comparison. This is achieved by XORing with a bias (see
234+
// pattern/_match.rs for another pertinent example of this pattern).
235+
//
236+
// Also, for performance, it's important to only do the second `try_to_bits` if
237+
// necessary.
238+
let lo = lo.try_to_bits(sz).unwrap() ^ bias;
239+
if lo <= min {
240+
let hi = hi.try_to_bits(sz).unwrap() ^ bias;
241+
if hi > max || hi == max && end == RangeEnd::Included {
239242
// Irrefutable pattern match.
240243
return Ok(());
241244
}

0 commit comments

Comments
 (0)