Skip to content

Commit dd63982

Browse files
committed
Revise the temp creation for blocks in stmt_expr to setup BlockTailInfo.
1 parent e7e1a52 commit dd63982

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/librustc_mir/build/expr/stmt.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
210210
// it is usually better to focus on `the_value` rather
211211
// than the entirety of block(s) surrounding it.
212212
let mut temp_span = expr_span;
213+
let mut temp_in_tail_of_block = false;
213214
if let ExprKind::Block { body } = expr.kind {
214215
if let Some(tail_expr) = &body.expr {
215216
let mut expr = tail_expr;
@@ -221,10 +222,25 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
221222
}
222223
}
223224
temp_span = expr.span;
225+
temp_in_tail_of_block = true;
224226
}
225227
}
226228

227-
let temp = this.temp(expr.ty.clone(), temp_span);
229+
let temp = {
230+
let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
231+
if temp_in_tail_of_block {
232+
if this.block_context.currently_ignores_tail_results() {
233+
local_decl = local_decl.block_tail(BlockTailInfo {
234+
tail_result_is_ignored: true
235+
});
236+
}
237+
}
238+
let temp = this.local_decls.push(local_decl);
239+
let place = Place::Local(temp);
240+
debug!("created temp {:?} for expr {:?} in block_context: {:?}",
241+
temp, expr, this.block_context);
242+
place
243+
};
228244
unpack!(block = this.into(&temp, block, expr));
229245

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

src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ LL | }
1212
LL |
1313
LL | ;
1414
| - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
15+
|
16+
= note: The temporary is part of an expression at the end of a block. Consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped.
1517

1618
error: aborting due to previous error
1719

0 commit comments

Comments
 (0)