diff options
| author | Shotaro Yamada <sinkuu@sinkuu.xyz> | 2018-12-15 22:44:39 +0900 |
|---|---|---|
| committer | Shotaro Yamada <sinkuu@sinkuu.xyz> | 2018-12-15 23:14:22 +0900 |
| commit | 2c1b1c26f49afcca779ec3d6d20e0cb4cbbad332 (patch) | |
| tree | c3506358212f36f582343c2a60fabef1281b3dca | |
| parent | 9daa823896402cde563a53934e821609783af835 (diff) | |
| download | rust-2c1b1c26f49afcca779ec3d6d20e0cb4cbbad332.tar.gz rust-2c1b1c26f49afcca779ec3d6d20e0cb4cbbad332.zip | |
Factor out
| -rw-r--r-- | src/librustc_mir/build/matches/test.rs | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index 77db74685cd..0abbfc540e6 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -138,13 +138,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { panic!("you should have called add_variants_to_switch instead!"); } PatternKind::Range { ty, lo, hi, end } => { - indices - .keys() - .all(|value| { - !self - .const_range_contains(ty, lo, hi, end, value) - .unwrap_or(true) - }) + // Check that none of the switch values are in the range. + self.values_not_contained_in_range(ty, lo, hi, end, indices) + .unwrap_or(false) } PatternKind::Slice { .. } | PatternKind::Array { .. } | @@ -541,16 +537,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { (&TestKind::SwitchInt { switch_ty: _, ref options, ref indices }, &PatternKind::Range { ty, lo, hi, end }) => { - let not_contained = indices - .keys() - .all(|value| { - !self - .const_range_contains(ty, lo, hi, end, value) - .unwrap_or(true) - }); + let not_contained = self + .values_not_contained_in_range(ty, lo, hi, end, indices) + .unwrap_or(false); if not_contained { - // No values are contained in the pattern range, + // No switch values are contained in the pattern range, // so the pattern can be matched only if this test fails. let otherwise = options.len(); resulting_candidates[otherwise].push(candidate.clone()); @@ -835,6 +827,23 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { _ => Some(false), } } + + fn values_not_contained_in_range( + &self, + ty: Ty<'tcx>, + lo: &'tcx ty::Const<'tcx>, + hi: &'tcx ty::Const<'tcx>, + end: RangeEnd, + indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>, + ) -> Option<bool> { + for val in indices.keys() { + if self.const_range_contains(ty, lo, hi, end, val)? { + return Some(false); + } + } + + Some(true) + } } fn is_switch_ty<'tcx>(ty: Ty<'tcx>) -> bool { |
