about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/mir
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-06 13:04:18 +0000
committerbors <bors@rust-lang.org>2021-12-06 13:04:18 +0000
commit0fb1c371d4a14f9ce7a721d8aea683a6e6774f6c (patch)
tree3dc1b51c056329110731a9da6e766e494302bb8d /compiler/rustc_middle/src/mir
parentbc9326d83d0d8368057ca8ff21c5260a4286f7ab (diff)
parentb215a32af361c0ac4a6670fa7878bcdc6ea12bbd (diff)
downloadrust-0fb1c371d4a14f9ce7a721d8aea683a6e6774f6c.tar.gz
rust-0fb1c371d4a14f9ce7a721d8aea683a6e6774f6c.zip
Auto merge of #91279 - scottmcm:small-refactor, r=nagisa
Small mir-opt refactor

Hopefully-non-controversial changes from some not-ready-yet work that I'd figured I'd submit on their own.
Diffstat (limited to 'compiler/rustc_middle/src/mir')
-rw-r--r--compiler/rustc_middle/src/mir/mod.rs1
-rw-r--r--compiler/rustc_middle/src/mir/terminator.rs7
2 files changed, 8 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs
index 75edcaadfdf..d4530883b6a 100644
--- a/compiler/rustc_middle/src/mir/mod.rs
+++ b/compiler/rustc_middle/src/mir/mod.rs
@@ -1519,6 +1519,7 @@ impl Statement<'_> {
     }
 
     /// Changes a statement to a nop and returns the original statement.
+    #[must_use = "If you don't need the statement, use `make_nop` instead"]
     pub fn replace_nop(&mut self) -> Self {
         Statement {
             source_info: self.source_info,
diff --git a/compiler/rustc_middle/src/mir/terminator.rs b/compiler/rustc_middle/src/mir/terminator.rs
index a82f98d28e7..d52b6a8bc75 100644
--- a/compiler/rustc_middle/src/mir/terminator.rs
+++ b/compiler/rustc_middle/src/mir/terminator.rs
@@ -78,6 +78,13 @@ impl SwitchTargets {
     pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
         &mut self.targets
     }
+
+    /// Finds the `BasicBlock` to which this `SwitchInt` will branch given the
+    /// specific value.  This cannot fail, as it'll return the `otherwise`
+    /// branch if there's not a specific match for the value.
+    pub fn target_for_value(&self, value: u128) -> BasicBlock {
+        self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
+    }
 }
 
 pub struct SwitchTargetsIter<'a> {