]> git.lizzy.rs Git - rust.git/commitdiff
Factor out
authorShotaro Yamada <sinkuu@sinkuu.xyz>
Sat, 15 Dec 2018 13:44:39 +0000 (22:44 +0900)
committerShotaro Yamada <sinkuu@sinkuu.xyz>
Sat, 15 Dec 2018 14:14:22 +0000 (23:14 +0900)
src/librustc_mir/build/matches/test.rs

index 77db74685cd2f8424219b8d2b88ee59488074e2c..0abbfc540e63ce285837d1481f02b87d86d63143 100644 (file)
@@ -138,13 +138,9 @@ pub fn add_cases_to_switch<'pat>(&mut self,
                 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 @@ pub fn sort_candidate<'pat>(&mut self,
 
             (&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 @@ fn const_range_contains(
             _ => 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 {