Skip to content

Commit 2c1b1c2

Browse files
committed
Factor out
1 parent 9daa823 commit 2c1b1c2

File tree

1 file changed

+24
-15
lines changed
  • src/librustc_mir/build/matches

1 file changed

+24
-15
lines changed

src/librustc_mir/build/matches/test.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
138138
panic!("you should have called add_variants_to_switch instead!");
139139
}
140140
PatternKind::Range { ty, lo, hi, end } => {
141-
indices
142-
.keys()
143-
.all(|value| {
144-
!self
145-
.const_range_contains(ty, lo, hi, end, value)
146-
.unwrap_or(true)
147-
})
141+
// Check that none of the switch values are in the range.
142+
self.values_not_contained_in_range(ty, lo, hi, end, indices)
143+
.unwrap_or(false)
148144
}
149145
PatternKind::Slice { .. } |
150146
PatternKind::Array { .. } |
@@ -541,16 +537,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
541537

542538
(&TestKind::SwitchInt { switch_ty: _, ref options, ref indices },
543539
&PatternKind::Range { ty, lo, hi, end }) => {
544-
let not_contained = indices
545-
.keys()
546-
.all(|value| {
547-
!self
548-
.const_range_contains(ty, lo, hi, end, value)
549-
.unwrap_or(true)
550-
});
540+
let not_contained = self
541+
.values_not_contained_in_range(ty, lo, hi, end, indices)
542+
.unwrap_or(false);
551543

552544
if not_contained {
553-
// No values are contained in the pattern range,
545+
// No switch values are contained in the pattern range,
554546
// so the pattern can be matched only if this test fails.
555547
let otherwise = options.len();
556548
resulting_candidates[otherwise].push(candidate.clone());
@@ -835,6 +827,23 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
835827
_ => Some(false),
836828
}
837829
}
830+
831+
fn values_not_contained_in_range(
832+
&self,
833+
ty: Ty<'tcx>,
834+
lo: &'tcx ty::Const<'tcx>,
835+
hi: &'tcx ty::Const<'tcx>,
836+
end: RangeEnd,
837+
indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>,
838+
) -> Option<bool> {
839+
for val in indices.keys() {
840+
if self.const_range_contains(ty, lo, hi, end, val)? {
841+
return Some(false);
842+
}
843+
}
844+
845+
Some(true)
846+
}
838847
}
839848

840849
fn is_switch_ty<'tcx>(ty: Ty<'tcx>) -> bool {

0 commit comments

Comments
 (0)