]> git.lizzy.rs Git - rust.git/commitdiff
set span more accurately during const_prop
authorRalf Jung <post@ralfj.de>
Sun, 5 Apr 2020 07:27:14 +0000 (09:27 +0200)
committerRalf Jung <post@ralfj.de>
Sun, 5 Apr 2020 17:28:28 +0000 (19:28 +0200)
src/librustc_mir/interpret/eval_context.rs
src/librustc_mir/interpret/step.rs
src/librustc_mir/transform/const_prop.rs

index 54403275ba6ad817b97ffb566bf297a80af2649b..3c5f091f46df7bc334df38cbcbc0b922d0d05488 100644 (file)
@@ -17,7 +17,7 @@
 use rustc_middle::ty::{
     self, fold::BottomUpFolder, query::TyCtxtAt, subst::SubstsRef, Ty, TyCtxt, TypeFoldable,
 };
-use rustc_span::source_map::DUMMY_SP;
+use rustc_span::{source_map::DUMMY_SP, Span};
 use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
 
 use super::{
@@ -296,6 +296,12 @@ pub fn new(
         }
     }
 
+    #[inline(always)]
+    pub(crate) fn set_span(&mut self, span: Span) {
+        self.tcx.span = span;
+        self.memory.tcx.span = span;
+    }
+
     #[inline(always)]
     pub fn force_ptr(
         &self,
index 2dd732e41eec2b1e7be03b86f11bf1e35c6c1ce2..37740878f7043365fb3270c4bed2cc55ccd137e2 100644 (file)
@@ -78,14 +78,13 @@ pub fn step(&mut self) -> InterpResult<'tcx, bool> {
 
     fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
         info!("{:?}", stmt);
+        self.set_span(stmt.source_info.span);
 
         use rustc_middle::mir::StatementKind::*;
 
         // Some statements (e.g., box) push new stack frames.
         // We have to record the stack frame number *before* executing the statement.
         let frame_idx = self.cur_frame();
-        self.tcx.span = stmt.source_info.span;
-        self.memory.tcx.span = stmt.source_info.span;
 
         match &stmt.kind {
             Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
@@ -276,8 +275,7 @@ pub fn eval_rvalue_into_place(
 
     fn terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
         info!("{:?}", terminator.kind);
-        self.tcx.span = terminator.source_info.span;
-        self.memory.tcx.span = terminator.source_info.span;
+        self.set_span(terminator.source_info.span);
 
         self.eval_terminator(terminator)?;
         if !self.stack.is_empty() {
index c7f63d24c28abeb3334415c3a9799aff84899e37..25719d037f9fc80cea7a7bbb0124329c86878a5d 100644 (file)
@@ -425,8 +425,6 @@ fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
     }
 
     fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
-        self.ecx.tcx.span = c.span;
-
         // FIXME we need to revisit this for #67176
         if c.needs_subst() {
             return None;
@@ -435,6 +433,8 @@ fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Opti
         match self.ecx.eval_const_to_op(c.literal, None) {
             Ok(op) => Some(op),
             Err(error) => {
+                // Make sure errors point at the constant.
+                self.ecx.set_span(c.span);
                 let err = error_to_const_error(&self.ecx, error);
                 if let Some(lint_root) = self.lint_root(source_info) {
                     let lint_only = match c.literal.val {
@@ -820,6 +820,7 @@ fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location)
     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
         trace!("visit_statement: {:?}", statement);
         let source_info = statement.source_info;
+        self.ecx.set_span(source_info.span);
         self.source_info = Some(source_info);
         if let StatementKind::Assign(box (place, ref mut rval)) = statement.kind {
             let place_ty: Ty<'tcx> = place.ty(&self.local_decls, self.tcx).ty;
@@ -870,6 +871,7 @@ fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Locatio
 
     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
         let source_info = terminator.source_info;
+        self.ecx.set_span(source_info.span);
         self.source_info = Some(source_info);
         self.super_terminator(terminator, location);
         match &mut terminator.kind {