about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-09-06 10:39:21 +0000
committerbors <bors@rust-lang.org>2021-09-06 10:39:21 +0000
commit1c858ba5bf7bd06c1a970efbf77053c8380b3151 (patch)
tree847426634bf6c1aaed58d2bde1942f1142adcde2 /compiler
parentd19d864e79531ad702e4f44b2635da6c50cc5eb0 (diff)
parentad7f109bfaa92f84bbcdbb5d376edfd8e66812fd (diff)
downloadrust-1c858ba5bf7bd06c1a970efbf77053c8380b3151.tar.gz
rust-1c858ba5bf7bd06c1a970efbf77053c8380b3151.zip
Auto merge of #88678 - matthewjasper:if-boolean-scoping, r=oli-obk
Change scope of temporaries in match guards

Each pattern in a match arm has its own copy of the match guard in MIR, with its own temporary, so it has to be dropped before the the guards are joined to the single copy of the arm. This PR changes `then_else_break` to allow it to put the temporary in the innermost scope possible. This change isn't done for `if` expressions because that affects a large number of mir-opt tests and could more significantly affect performance.

closes #88649

r? `@oli-obk`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_mir_build/src/build/expr/into.rs2
-rw-r--r--compiler/rustc_mir_build/src/build/matches/mod.rs8
2 files changed, 5 insertions, 5 deletions
diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs
index 22c44beb350..e30e758e637 100644
--- a/compiler/rustc_mir_build/src/build/expr/into.rs
+++ b/compiler/rustc_mir_build/src/build/expr/into.rs
@@ -68,7 +68,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                                     let then_blk = unpack!(this.then_else_break(
                                         block,
                                         &this.thir[cond],
-                                        condition_scope,
+                                        Some(condition_scope),
                                         condition_scope,
                                         then_expr.span,
                                     ));
diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs
index ec54a2a0ec4..ba94e15444a 100644
--- a/compiler/rustc_mir_build/src/build/matches/mod.rs
+++ b/compiler/rustc_mir_build/src/build/matches/mod.rs
@@ -39,7 +39,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
         &mut self,
         mut block: BasicBlock,
         expr: &Expr<'tcx>,
-        temp_scope: region::Scope,
+        temp_scope_override: Option<region::Scope>,
         break_scope: region::Scope,
         variable_scope_span: Span,
     ) -> BlockAnd<()> {
@@ -53,7 +53,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                     this.then_else_break(
                         block,
                         &this.thir[value],
-                        temp_scope,
+                        temp_scope_override,
                         break_scope,
                         variable_scope_span,
                     )
@@ -63,6 +63,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                 this.lower_let_expr(block, &this.thir[expr], pat, break_scope, variable_scope_span)
             }
             _ => {
+                let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope());
                 let mutability = Mutability::Mut;
                 let place =
                     unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability));
@@ -1948,7 +1949,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
             }
 
             let arm_span = arm_span.unwrap();
-            let arm_scope = self.local_scope();
             let match_scope = match_scope.unwrap();
             let mut guard_span = rustc_span::DUMMY_SP;
 
@@ -1957,7 +1957,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                     Guard::If(e) => {
                         let e = &this.thir[e];
                         guard_span = e.span;
-                        this.then_else_break(block, e, arm_scope, match_scope, arm_span)
+                        this.then_else_break(block, e, None, match_scope, arm_span)
                     }
                     Guard::IfLet(ref pat, scrutinee) => {
                         let s = &this.thir[scrutinee];