diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 9a3ccc2f7e4..afc5f08b2d2 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -17,7 +17,7 @@ use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout}; use rustc_middle::ty::InternalSubsts; use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisitableExt}; -use rustc_span::{def_id::DefId, Span}; +use rustc_span::{def_id::DefId, Span, DUMMY_SP}; use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout}; use rustc_target::spec::abi::Abi as CallAbi; use rustc_trait_selection::traits; @@ -328,9 +328,6 @@ struct ConstPropagator<'mir, 'tcx> { tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, local_decls: &'mir IndexVec>, - // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store - // the last known `SourceInfo` here and just keep revisiting it. - source_info: Option, } impl<'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'_, 'tcx> { @@ -411,13 +408,7 @@ fn new( ) .expect("failed to push initial stack frame"); - ConstPropagator { - ecx, - tcx, - param_env, - local_decls: &dummy_body.local_decls, - source_info: None, - } + ConstPropagator { ecx, tcx, param_env, local_decls: &dummy_body.local_decls } } fn get_const(&self, place: Place<'tcx>) -> Option> { @@ -495,7 +486,7 @@ fn propagate_operand(&mut self, operand: &mut Operand<'tcx>) { *operand = self.operand_from_scalar( scalar, value.layout.ty, - self.source_info.unwrap().span, + DUMMY_SP, ); } } @@ -629,12 +620,7 @@ fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Opera })) } - fn replace_with_const( - &mut self, - rval: &mut Rvalue<'tcx>, - value: &OpTy<'tcx>, - source_info: SourceInfo, - ) { + fn replace_with_const(&mut self, rval: &mut Rvalue<'tcx>, value: &OpTy<'tcx>) { if let Rvalue::Use(Operand::Constant(c)) = rval { match c.literal { ConstantKind::Ty(c) if matches!(c.kind(), ConstKind::Unevaluated(..)) => {} @@ -664,11 +650,8 @@ fn replace_with_const( if let Some(Right(imm)) = imm { match *imm { interpret::Immediate::Scalar(scalar) => { - *rval = Rvalue::Use(self.operand_from_scalar( - scalar, - value.layout.ty, - source_info.span, - )); + *rval = + Rvalue::Use(self.operand_from_scalar(scalar, value.layout.ty, DUMMY_SP)); } Immediate::ScalarPair(..) => { // Found a value represented as a pair. For now only do const-prop if the type @@ -701,7 +684,7 @@ fn replace_with_const( let const_val = ConstValue::ByRef { alloc, offset: Size::ZERO }; let literal = ConstantKind::Val(const_val, ty); *rval = Rvalue::Use(Operand::Constant(Box::new(Constant { - span: source_info.span, + span: DUMMY_SP, user_ty: None, literal, }))); @@ -894,8 +877,6 @@ 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.source_info = Some(source_info); match statement.kind { StatementKind::Assign(box (place, ref mut rval)) => { let can_const_prop = self.ecx.machine.can_const_prop[place.local]; @@ -905,7 +886,7 @@ fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Locatio // consists solely of uninitialized memory (so it doesn't capture any locals). if let Some(ref value) = self.get_const(place) && self.should_const_prop(value) { trace!("replacing {:?} with {:?}", rval, value); - self.replace_with_const(rval, value, source_info); + self.replace_with_const(rval, value); if can_const_prop == ConstPropMode::FullConstProp || can_const_prop == ConstPropMode::OnlyInsideOwnBlock { @@ -977,8 +958,6 @@ 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.source_info = Some(source_info); self.super_terminator(terminator, location); match &mut terminator.kind { @@ -991,7 +970,7 @@ fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Loca *cond = self.operand_from_scalar( value_const, self.tcx.types.bool, - source_info.span, + DUMMY_SP, ); } } diff --git a/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff b/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff index b9a10704be0..6d8738aa61a 100644 --- a/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff +++ b/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff @@ -22,7 +22,7 @@ - switchInt(move _4) -> [1: bb1, otherwise: bb3]; // scope 2 at $DIR/discriminant.rs:+1:21: +1:31 + _3 = const Option::::Some(true); // scope 2 at $DIR/discriminant.rs:+1:34: +1:44 + // mir::Constant -+ // + span: $DIR/discriminant.rs:12:34: 12:44 ++ // + span: no-location + // + literal: Const { ty: Option, val: Value(Scalar(0x01)) } + _4 = const 1_isize; // scope 2 at $DIR/discriminant.rs:+1:21: +1:31 + switchInt(const 1_isize) -> [1: bb1, otherwise: bb3]; // scope 2 at $DIR/discriminant.rs:+1:21: +1:31 diff --git a/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff b/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff index 4f056dd85e3..a38c1de2a78 100644 --- a/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff @@ -44,11 +44,11 @@ - _3 = [move _4]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 + _4 = const Scalar(0x00000004): E; // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:57 + // mir::Constant -+ // + span: $DIR/invalid_constant.rs:28:34: 28:57 ++ // + span: no-location + // + literal: Const { ty: E, val: Value(Scalar(0x00000004)) } + _3 = [const Scalar(0x00000004): E]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 + // mir::Constant -+ // + span: $DIR/invalid_constant.rs:28:24: 28:60 ++ // + span: no-location + // + literal: Const { ty: E, val: Value(Scalar(0x00000004)) } StorageDead(_4); // scope 1 at $DIR/invalid_constant.rs:+13:59: +13:60 StorageDead(_5); // scope 1 at $DIR/invalid_constant.rs:+13:60: +13:61 diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff index 1f5c533815d..ec063294856 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff @@ -54,7 +54,7 @@ - _6 = MinusPlus; // scope 1 at $DIR/funky_arms.rs:+10:17: +10:41 + _6 = const MinusPlus; // scope 1 at $DIR/funky_arms.rs:+10:17: +10:41 + // mir::Constant -+ // + span: $DIR/funky_arms.rs:21:17: 21:41 ++ // + span: no-location + // + literal: Const { ty: Sign, val: Value(Scalar(0x01)) } goto -> bb4; // scope 1 at $DIR/funky_arms.rs:+10:17: +10:41 } @@ -63,7 +63,7 @@ - _6 = Minus; // scope 1 at $DIR/funky_arms.rs:+9:18: +9:38 + _6 = const Minus; // scope 1 at $DIR/funky_arms.rs:+9:18: +9:38 + // mir::Constant -+ // + span: $DIR/funky_arms.rs:20:18: 20:38 ++ // + span: no-location + // + literal: Const { ty: Sign, val: Value(Scalar(0x00)) } goto -> bb4; // scope 1 at $DIR/funky_arms.rs:+9:18: +9:38 }