From 3011ecdeac214aeb99714abbefffbf6a1fe1e0c8 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 8 Nov 2018 12:39:54 +0100 Subject: [PATCH] Narrow span of temp holding the value of a Block expression to the block's tail (if present). --- src/librustc_mir/build/expr/stmt.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 @@ pub fn stmt_expr(&mut self, } _ => { 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