Skip to content

Commit 3011ecd

Browse files
committed
Narrow span of temp holding the value of a Block expression to the block's tail (if present).
1 parent ece4f47 commit 3011ecd

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/librustc_mir/build/expr/stmt.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,30 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
201201
}
202202
_ => {
203203
let expr_ty = expr.ty;
204-
let temp = this.temp(expr.ty.clone(), expr_span);
204+
205+
// Issue #54382: When creating temp for the value of
206+
// expression like:
207+
//
208+
// `{ side_effects(); { let l = stuff(); the_value } }`
209+
//
210+
// it is usually better to focus on `the_value` rather
211+
// than the entirety of block(s) surrounding it.
212+
let mut temp_span = expr_span;
213+
if let ExprKind::Block { body } = expr.kind {
214+
if let Some(tail_expr) = &body.expr {
215+
let mut expr = tail_expr;
216+
while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
217+
if let Some(subtail_expr) = &subblock.expr {
218+
expr = subtail_expr
219+
} else {
220+
break;
221+
}
222+
}
223+
temp_span = expr.span;
224+
}
225+
}
226+
227+
let temp = this.temp(expr.ty.clone(), temp_span);
205228
unpack!(block = this.into(&temp, block, expr));
206229

207230
// Attribute drops of the statement's temps to the

0 commit comments

Comments
 (0)