]> git.lizzy.rs Git - rust.git/commitdiff
misc cleanup in match MIR building
authorMazdak Farrokhzad <twingoow@gmail.com>
Fri, 20 Dec 2019 17:05:45 +0000 (18:05 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Dec 2019 21:10:16 +0000 (22:10 +0100)
src/librustc_mir/build/matches/mod.rs
src/librustc_mir/build/matches/test.rs

index bf0b2439c00b5924119452edd3dba08b2f96b519..c0a7439b5ce57eb0b6fb8180b5fc1c876c36b15f 100644 (file)
@@ -26,6 +26,7 @@
 mod test;
 mod util;
 
+use itertools::Itertools;
 use std::convert::TryFrom;
 
 impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -822,9 +823,7 @@ fn match_candidates<'pat>(
         );
         let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
 
-        let block: BasicBlock;
-
-        if !matched_candidates.is_empty() {
+        let block: BasicBlock = if !matched_candidates.is_empty() {
             let otherwise_block = self.select_matched_candidates(
                 matched_candidates,
                 start_block,
@@ -832,17 +831,17 @@ fn match_candidates<'pat>(
             );
 
             if let Some(last_otherwise_block) = otherwise_block {
-                block = last_otherwise_block
+                last_otherwise_block
             } else {
                 // Any remaining candidates are unreachable.
                 if unmatched_candidates.is_empty() {
                     return;
                 }
-                block = self.cfg.start_new_block();
-            };
+                self.cfg.start_new_block()
+            }
         } else {
-            block = *start_block.get_or_insert_with(|| self.cfg.start_new_block());
-        }
+            *start_block.get_or_insert_with(|| self.cfg.start_new_block())
+        };
 
         // If there are no candidates that still need testing, we're
         // done. Since all matches are exhaustive, execution should
@@ -885,7 +884,7 @@ fn match_candidates<'pat>(
     /// ...
     ///
     /// We generate real edges from:
-    /// * `block` to the prebinding_block of the first pattern,
+    /// * `start_block` to the `prebinding_block` of the first pattern,
     /// * the otherwise block of the first pattern to the second pattern,
     /// * the otherwise block of the third pattern to the a block with an
     ///   Unreachable terminator.
@@ -948,6 +947,7 @@ fn select_matched_candidates(
         let first_candidate = &reachable_candidates[0];
         let first_prebinding_block = first_candidate.pre_binding_block;
 
+        // `goto -> first_prebinding_block` from the `start_block` if there is one.
         if let Some(start_block) = *start_block {
             let source_info = self.source_info(first_candidate.span);
             self.cfg.terminate(
@@ -959,21 +959,17 @@ fn select_matched_candidates(
             *start_block = Some(first_prebinding_block);
         }
 
-        for window in reachable_candidates.windows(2) {
-            if let [first_candidate, second_candidate] = window {
-                let source_info = self.source_info(first_candidate.span);
-                if let Some(otherwise_block) = first_candidate.otherwise_block {
-                    self.false_edges(
-                        otherwise_block,
-                        second_candidate.pre_binding_block,
-                        first_candidate.next_candidate_pre_binding_block,
-                        source_info,
-                    );
-                } else {
-                    bug!("candidate other than the last has no guard");
-                }
+        for (first_candidate, second_candidate) in reachable_candidates.iter().tuple_windows() {
+            let source_info = self.source_info(first_candidate.span);
+            if let Some(otherwise_block) = first_candidate.otherwise_block {
+                self.false_edges(
+                    otherwise_block,
+                    second_candidate.pre_binding_block,
+                    first_candidate.next_candidate_pre_binding_block,
+                    source_info,
+                );
             } else {
-                bug!("<[_]>::windows returned incorrectly sized window");
+                bug!("candidate other than the last has no guard");
             }
         }
 
index e320811ca0556e10ef1519c9997c23b32c238f5d..bdc1bdd5b98552a71ba1fd74db1b766ee7ec79c6 100644 (file)
@@ -30,7 +30,7 @@ pub fn test<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> Test<'tcx> {
                 Test {
                     span: match_pair.pattern.span,
                     kind: TestKind::Switch {
-                        adt_def: adt_def.clone(),
+                        adt_def,
                         variants: BitSet::new_empty(adt_def.variants.len()),
                     },
                 }