Skip to content

Commit 43cdfbb

Browse files
committed
set span more accurately during const_prop
1 parent fb9b693 commit 43cdfbb

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/librustc_mir/interpret/eval_context.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::layout::{self, TyAndLayout};
1717
use rustc_middle::ty::{
1818
self, fold::BottomUpFolder, query::TyCtxtAt, subst::SubstsRef, Ty, TyCtxt, TypeFoldable,
1919
};
20-
use rustc_span::source_map::DUMMY_SP;
20+
use rustc_span::{source_map::DUMMY_SP, Span};
2121
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
2222

2323
use super::{
@@ -296,6 +296,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
296296
}
297297
}
298298

299+
#[inline(always)]
300+
pub(crate) fn set_span(&mut self, span: Span) {
301+
self.tcx.span = span;
302+
self.memory.tcx.span = span;
303+
}
304+
299305
#[inline(always)]
300306
pub fn force_ptr(
301307
&self,

src/librustc_mir/interpret/step.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
7878

7979
fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
8080
info!("{:?}", stmt);
81+
self.set_span(stmt.source_info.span);
8182

8283
use rustc_middle::mir::StatementKind::*;
8384

8485
// Some statements (e.g., box) push new stack frames.
8586
// We have to record the stack frame number *before* executing the statement.
8687
let frame_idx = self.cur_frame();
87-
self.tcx.span = stmt.source_info.span;
88-
self.memory.tcx.span = stmt.source_info.span;
8988

9089
match &stmt.kind {
9190
Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
@@ -276,8 +275,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
276275

277276
fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
278277
info!("{:?}", terminator.kind);
279-
self.tcx.span = terminator.source_info.span;
280-
self.memory.tcx.span = terminator.source_info.span;
278+
self.set_span(terminator.source_info.span);
281279

282280
self.eval_terminator(terminator)?;
283281
if !self.stack.is_empty() {

src/librustc_mir/transform/const_prop.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
425425
}
426426

427427
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
428-
self.ecx.tcx.span = c.span;
429-
430428
// FIXME we need to revisit this for #67176
431429
if c.needs_subst() {
432430
return None;
@@ -435,6 +433,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
435433
match self.ecx.eval_const_to_op(c.literal, None) {
436434
Ok(op) => Some(op),
437435
Err(error) => {
436+
// Make sure errors point at the constant.
437+
self.ecx.set_span(c.span);
438438
let err = error_to_const_error(&self.ecx, error);
439439
if let Some(lint_root) = self.lint_root(source_info) {
440440
let lint_only = match c.literal.val {
@@ -820,6 +820,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
820820
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
821821
trace!("visit_statement: {:?}", statement);
822822
let source_info = statement.source_info;
823+
self.ecx.set_span(source_info.span);
823824
self.source_info = Some(source_info);
824825
if let StatementKind::Assign(box (place, ref mut rval)) = statement.kind {
825826
let place_ty: Ty<'tcx> = place.ty(&self.local_decls, self.tcx).ty;
@@ -870,6 +871,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
870871

871872
fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
872873
let source_info = terminator.source_info;
874+
self.ecx.set_span(source_info.span);
873875
self.source_info = Some(source_info);
874876
self.super_terminator(terminator, location);
875877
match &mut terminator.kind {

0 commit comments

Comments
 (0)