Skip to content

Commit ece4f47

Browse files
committed
For diagnostics, set spans of drops of temps to be that of the statement's terminating semicolon.
1 parent 1d83455 commit ece4f47

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

src/librustc_mir/build/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9090

9191
let source_info = this.source_info(span);
9292
for stmt in stmts {
93-
let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
93+
let Stmt { kind, opt_destruction_scope, span: stmt_span } = this.hir.mirror(stmt);
9494
match kind {
9595
StmtKind::Expr { scope, expr } => {
9696
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
@@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9999
let si = (scope, source_info);
100100
this.in_scope(si, LintLevel::Inherited, block, |this| {
101101
let expr = this.hir.mirror(expr);
102-
this.stmt_expr(block, expr)
102+
this.stmt_expr(block, expr, Some(stmt_span))
103103
})
104104
}));
105105
}

src/librustc_mir/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
351351
block.and(Rvalue::Aggregate(adt, fields))
352352
}
353353
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
354-
block = unpack!(this.stmt_expr(block, expr));
354+
block = unpack!(this.stmt_expr(block, expr, None));
355355
block.and(this.unit_rvalue())
356356
}
357357
ExprKind::Yield { value } => {

src/librustc_mir/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
351351
| ExprKind::Break { .. }
352352
| ExprKind::InlineAsm { .. }
353353
| ExprKind::Return { .. } => {
354-
unpack!(block = this.stmt_expr(block, expr));
354+
unpack!(block = this.stmt_expr(block, expr, None));
355355
this.cfg.push_assign_unit(block, source_info, destination);
356356
block.unit()
357357
}

src/librustc_mir/build/expr/stmt.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ use hair::*;
1414
use rustc::mir::*;
1515

1616
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17-
pub fn stmt_expr(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<()> {
17+
/// Builds a block of MIR statements to evaluate the HAIR `expr`.
18+
/// If the original expression was an AST statement,
19+
/// (e.g. `some().code(&here());`) then `opt_stmt_span` is the
20+
/// span of that statement (including its semicolon, if any).
21+
/// Diagnostics use this span (which may be larger than that of
22+
/// `expr`) to identify when statement temporaries are dropped.
23+
pub fn stmt_expr(&mut self,
24+
mut block: BasicBlock,
25+
expr: Expr<'tcx>,
26+
opt_stmt_span: Option<StatementSpan>)
27+
-> BlockAnd<()>
28+
{
1829
let this = self;
1930
let expr_span = expr.span;
2031
let source_info = this.source_info(expr.span);
@@ -29,7 +40,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2940
} => {
3041
let value = this.hir.mirror(value);
3142
this.in_scope((region_scope, source_info), lint_level, block, |this| {
32-
this.stmt_expr(block, value)
43+
this.stmt_expr(block, value, opt_stmt_span)
3344
})
3445
}
3546
ExprKind::Assign { lhs, rhs } => {
@@ -192,7 +203,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
192203
let expr_ty = expr.ty;
193204
let temp = this.temp(expr.ty.clone(), expr_span);
194205
unpack!(block = this.into(&temp, block, expr));
195-
unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));
206+
207+
// Attribute drops of the statement's temps to the
208+
// semicolon at the statement's end.
209+
let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
210+
None => expr_span,
211+
Some(StatementSpan(span)) => span,
212+
});
213+
214+
unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));
196215
block.unit()
197216
}
198217
}

src/librustc_mir/hair/cx/block.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
5757
for (index, stmt) in stmts.iter().enumerate() {
5858
let hir_id = cx.tcx.hir.node_to_hir_id(stmt.node.id());
5959
let opt_dxn_ext = cx.region_scope_tree.opt_destruction_scope(hir_id.local_id);
60+
let stmt_span = StatementSpan(cx.tcx.hir.span(stmt.node.id()));
6061
match stmt.node {
6162
hir::StmtKind::Expr(ref expr, _) |
6263
hir::StmtKind::Semi(ref expr, _) => {
@@ -69,6 +70,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
6970
expr: expr.to_ref(),
7071
},
7172
opt_destruction_scope: opt_dxn_ext,
73+
span: stmt_span,
7274
})))
7375
}
7476
hir::StmtKind::Decl(ref decl, _) => {
@@ -111,6 +113,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
111113
lint_level: cx.lint_level_of(local.id),
112114
},
113115
opt_destruction_scope: opt_dxn_ext,
116+
span: stmt_span,
114117
})));
115118
}
116119
}

src/librustc_mir/hair/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ pub enum StmtRef<'tcx> {
7171
Mirror(Box<Stmt<'tcx>>),
7272
}
7373

74+
#[derive(Clone, Debug)]
75+
pub struct StatementSpan(pub Span);
76+
7477
#[derive(Clone, Debug)]
7578
pub struct Stmt<'tcx> {
7679
pub kind: StmtKind<'tcx>,
7780
pub opt_destruction_scope: Option<region::Scope>,
81+
pub span: StatementSpan,
7882
}
7983

8084
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)