Skip to content

Commit c00fd8f

Browse files
committed
Refactor interval conditions
1 parent 7f72030 commit c00fd8f

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,7 @@ fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
620620

621621
/// An inclusive interval, used for precise integer exhaustiveness checking.
622622
struct Interval<'tcx> {
623-
pub lo: u128,
624-
pub hi: u128,
623+
pub range: RangeInclusive<u128>,
625624
pub ty: Ty<'tcx>,
626625
}
627626

@@ -641,15 +640,15 @@ impl<'tcx> Interval<'tcx> {
641640
None
642641
} else {
643642
let offset = (*end == RangeEnd::Excluded) as u128;
644-
Some(Interval { lo, hi: hi - offset, ty })
643+
Some(Interval { range: lo..=(hi - offset), ty })
645644
};
646645
}
647646
}
648647
None
649648
}
650649
ConstantValue(val) => {
651650
let ty = val.ty;
652-
val.assert_bits(ty).map(|val| Interval { lo: val, hi: val, ty })
651+
val.assert_bits(ty).map(|val| Interval { range: val..=val, ty })
653652
}
654653
Single | Variant(_) | Slice(_) => {
655654
None
@@ -690,7 +689,7 @@ impl<'tcx> Interval<'tcx> {
690689
}
691690

692691
fn into_inner(self) -> (u128, u128) {
693-
(self.lo, self.hi)
692+
self.range.into_inner()
694693
}
695694
}
696695

@@ -706,31 +705,27 @@ fn ranges_subtract_pattern<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
706705
let mut ranges: Vec<_> = ranges.into_iter().filter_map(|r| {
707706
Interval::from_ctor(&r).map(|i| i.into_inner())
708707
}).collect();
708+
let ty = pat_interval.ty;
709+
let (pat_interval_lo, pat_interval_hi) = pat_interval.into_inner();
709710
for (subrange_lo, subrange_hi) in ranges {
710-
if pat_interval.lo > subrange_hi || pat_interval.hi < subrange_lo {
711+
if pat_interval_lo > subrange_hi || subrange_lo > pat_interval_hi {
711712
// The pattern doesn't intersect with the subrange at all,
712713
// so the subrange remains untouched.
713714
remaining_ranges.push((subrange_lo, subrange_hi));
714-
} else if pat_interval.lo <= subrange_lo && pat_interval.hi >= subrange_hi {
715-
// The pattern entirely covers the subrange of values,
716-
// so we no longer have to consider this subrange_
717-
} else if pat_interval.lo <= subrange_lo {
718-
// The pattern intersects a lower section of the subrange,
719-
// so only the upper section will remain.
720-
remaining_ranges.push((pat_interval.hi + 1, subrange_hi));
721-
} else if pat_interval.hi >= subrange_hi {
722-
// The pattern intersects an upper section of the subrange,
723-
// so only the lower section will remain.
724-
remaining_ranges.push((subrange_lo, pat_interval.lo - 1));
725715
} else {
726-
// The pattern intersects the middle of the subrange,
727-
// so we create two ranges either side of the intersection.)
728-
remaining_ranges.push((subrange_lo, pat_interval.lo - 1));
729-
remaining_ranges.push((pat_interval.hi + 1, subrange_hi));
716+
if pat_interval_lo > subrange_lo {
717+
// The pattern intersects an upper section of the
718+
// subrange, so a lower section will remain.
719+
remaining_ranges.push((subrange_lo, pat_interval_lo - 1));
720+
}
721+
if pat_interval_hi < subrange_hi {
722+
// The pattern intersects a lower section of the
723+
// subrange, so an upper section will remain.
724+
remaining_ranges.push((pat_interval_hi + 1, subrange_hi));
725+
}
730726
}
731727
}
732728
// Convert the remaining ranges from pairs to inclusive `ConstantRange`s.
733-
let ty = pat_interval.ty;
734729
remaining_ranges.into_iter().map(|(lo, hi)| {
735730
let (lo, hi) = Interval::offset_sign(ty, (lo, hi), false);
736731
ConstantRange(ty::Const::from_bits(cx.tcx, lo, ty),
@@ -839,7 +834,7 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
839834
// `missing_ctors` are those that should have appeared
840835
// as patterns in the `match` expression, but did not.
841836
let mut missing_ctors = vec![];
842-
'req: for req_ctor in all_ctors.clone() {
837+
'req: for req_ctor in &all_ctors {
843838
let mut sub_ctors = vec![req_ctor.clone()];
844839
// The only constructor patterns for which it is valid to
845840
// treat the values as constructors are ranges (see
@@ -861,7 +856,7 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
861856
// If the pattern for the required constructor
862857
// appears in the `match`, then it is not missing,
863858
// and we can move on to the next one.
864-
if *used_ctor == req_ctor {
859+
if used_ctor == req_ctor {
865860
continue 'req;
866861
}
867862
}

0 commit comments

Comments
 (0)