about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2018-11-08 12:39:54 +0100
committerFelix S. Klock II <pnkfelix@pnkfx.org>2018-11-08 12:54:20 +0100
commit3011ecdeac214aeb99714abbefffbf6a1fe1e0c8 (patch)
tree2246e4ac5ea074c1244d2d502902ef5aee7c041b
parentece4f472c923d72faf50efaaba60a8f51c143bec (diff)
downloadrust-3011ecdeac214aeb99714abbefffbf6a1fe1e0c8.tar.gz
rust-3011ecdeac214aeb99714abbefffbf6a1fe1e0c8.zip
Narrow span of temp holding the value of a Block expression to the block's tail (if present).
-rw-r--r--src/librustc_mir/build/expr/stmt.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/librustc_mir/build/expr/stmt.rs b/src/librustc_mir/build/expr/stmt.rs
index 3b9fb7237b0..708ddd3842d 100644
--- a/src/librustc_mir/build/expr/stmt.rs
+++ b/src/librustc_mir/build/expr/stmt.rs
@@ -201,7 +201,30 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
             }
             _ => {
                 let expr_ty = expr.ty;
-                let temp = this.temp(expr.ty.clone(), expr_span);
+
+                // Issue #54382: When creating temp for the value of
+                // expression like:
+                //
+                // `{ side_effects(); { let l = stuff(); the_value } }`
+                //
+                // it is usually better to focus on `the_value` rather
+                // than the entirety of block(s) surrounding it.
+                let mut temp_span = expr_span;
+                if let ExprKind::Block { body } = expr.kind {
+                    if let Some(tail_expr) = &body.expr {
+                        let mut expr = tail_expr;
+                        while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
+                            if let Some(subtail_expr) = &subblock.expr {
+                                expr = subtail_expr
+                            } else {
+                                break;
+                            }
+                        }
+                        temp_span = expr.span;
+                    }
+                }
+
+                let temp = this.temp(expr.ty.clone(), temp_span);
                 unpack!(block = this.into(&temp, block, expr));
 
                 // Attribute drops of the statement's temps to the