about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_mir/build/expr/stmt.rs18
-rw-r--r--src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr2
2 files changed, 19 insertions, 1 deletions
diff --git a/src/librustc_mir/build/expr/stmt.rs b/src/librustc_mir/build/expr/stmt.rs
index 708ddd3842d..45235b31539 100644
--- a/src/librustc_mir/build/expr/stmt.rs
+++ b/src/librustc_mir/build/expr/stmt.rs
@@ -210,6 +210,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
                 // it is usually better to focus on `the_value` rather
                 // than the entirety of block(s) surrounding it.
                 let mut temp_span = expr_span;
+                let mut temp_in_tail_of_block = false;
                 if let ExprKind::Block { body } = expr.kind {
                     if let Some(tail_expr) = &body.expr {
                         let mut expr = tail_expr;
@@ -221,10 +222,25 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
                             }
                         }
                         temp_span = expr.span;
+                        temp_in_tail_of_block = true;
                     }
                 }
 
-                let temp = this.temp(expr.ty.clone(), temp_span);
+                let temp = {
+                    let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
+                    if temp_in_tail_of_block {
+                        if this.block_context.currently_ignores_tail_results() {
+                            local_decl = local_decl.block_tail(BlockTailInfo {
+                                tail_result_is_ignored: true
+                            });
+                        }
+                    }
+                    let temp = this.local_decls.push(local_decl);
+                    let place = Place::Local(temp);
+                    debug!("created temp {:?} for expr {:?} in block_context: {:?}",
+                           temp, expr, this.block_context);
+                    place
+                };
                 unpack!(block = this.into(&temp, block, expr));
 
                 // Attribute drops of the statement's temps to the
diff --git a/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr
index 31f49b2836e..c308562c0cc 100644
--- a/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr
+++ b/src/test/ui/nll/issue-54382-use-span-of-tail-of-block.nll.stderr
@@ -12,6 +12,8 @@ LL |     }
 LL | 
 LL |     ;
    |     - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
+   |
+   = 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.
 
 error: aborting due to previous error