Skip to content

Commit 309e520

Browse files
committed
Remove location threading
1 parent d0e1c2c commit 309e520

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
44
use std::fmt::Debug;
55

6-
use either::Left;
7-
86
use rustc_const_eval::interpret::{ImmTy, MPlaceTy, Projectable};
97
use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar, StackPopCleanup};
108
use rustc_hir::def::DefKind;
@@ -248,12 +246,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
248246
source_info.scope.lint_root(&self.body().source_scopes)
249247
}
250248

251-
fn use_ecx<F, T>(&mut self, location: Location, f: F) -> Option<T>
249+
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
252250
where
253251
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
254252
{
255-
// Overwrite the PC -- whatever the interpreter does to it does not make any sense anyway.
256-
self.ecx.frame_mut().loc = Left(location);
257253
match f(self) {
258254
Ok(val) => Some(val),
259255
Err(error) => {
@@ -275,7 +271,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
275271
fn eval_constant(
276272
&mut self,
277273
c: &ConstOperand<'tcx>,
278-
location: Location,
279274
layout: Option<TyAndLayout<'tcx>>,
280275
) -> Option<OpTy<'tcx>> {
281276
// FIXME we need to revisit this for #67176
@@ -291,7 +286,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
291286
// manually normalized.
292287
let val = self.tcx.try_normalize_erasing_regions(self.param_env, c.const_).ok()?;
293288

294-
self.use_ecx(location, |this| this.ecx.eval_mir_constant(&val, Some(c.span), layout))
289+
self.use_ecx(|this| this.ecx.eval_mir_constant(&val, Some(c.span), layout))
295290
}
296291

297292
/// Returns the value, if any, of evaluating `place`.
@@ -313,11 +308,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
313308
fn eval_operand(
314309
&mut self,
315310
op: &Operand<'tcx>,
316-
location: Location,
317311
layout: Option<TyAndLayout<'tcx>>,
318312
) -> Option<OpTy<'tcx>> {
319313
match *op {
320-
Operand::Constant(ref c) => self.eval_constant(c, location, layout),
314+
Operand::Constant(ref c) => self.eval_constant(c, layout),
321315
Operand::Move(place) | Operand::Copy(place) => self.eval_place(place, layout),
322316
}
323317
}
@@ -329,8 +323,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
329323
}
330324

331325
fn check_unary_op(&mut self, op: UnOp, arg: &Operand<'tcx>, location: Location) -> Option<()> {
332-
let arg = self.eval_operand(arg, location, None)?;
333-
if let (val, true) = self.use_ecx(location, |this| {
326+
let arg = self.eval_operand(arg, None)?;
327+
if let (val, true) = self.use_ecx(|this| {
334328
let val = this.ecx.read_immediate(&arg)?;
335329
let (_res, overflow) = this.ecx.overflowing_unary_op(op, &val)?;
336330
Ok((val, overflow))
@@ -360,11 +354,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
360354
location: Location,
361355
) -> Option<()> {
362356
let r = self
363-
.eval_operand(right, location, None)
364-
.and_then(|r| self.use_ecx(location, |this| this.ecx.read_immediate(&r)));
357+
.eval_operand(right, None)
358+
.and_then(|r| self.use_ecx(|this| this.ecx.read_immediate(&r)));
365359
let l = self
366-
.eval_operand(left, location, None)
367-
.and_then(|l| self.use_ecx(location, |this| this.ecx.read_immediate(&l)));
360+
.eval_operand(left, None)
361+
.and_then(|l| self.use_ecx(|this| this.ecx.read_immediate(&l)));
368362
// Check for exceeding shifts *even if* we cannot evaluate the LHS.
369363
if matches!(op, BinOp::Shr | BinOp::Shl) {
370364
let r = r.clone()?;
@@ -400,7 +394,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
400394

401395
if let (Some(l), Some(r)) = (l, r) {
402396
// The remaining operators are handled through `overflowing_binary_op`.
403-
if self.use_ecx(location, |this| {
397+
if self.use_ecx(|this| {
404398
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
405399
Ok(overflow)
406400
})? {
@@ -501,11 +495,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
501495
cond: &Operand<'tcx>,
502496
location: Location,
503497
) -> Option<!> {
504-
let value = &self.eval_operand(cond, location, None)?;
498+
let value = &self.eval_operand(cond, None)?;
505499
trace!("assertion on {:?} should be {:?}", value, expected);
506500

507501
let expected = Scalar::from_bool(expected);
508-
let value_const = self.use_ecx(location, |this| this.ecx.read_scalar(value))?;
502+
let value_const = self.use_ecx(|this| this.ecx.read_scalar(value))?;
509503

510504
if expected != value_const {
511505
// Poison all places this operand references so that further code
@@ -529,7 +523,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
529523
let mut eval_to_int = |op| {
530524
// This can be `None` if the lhs wasn't const propagated and we just
531525
// triggered the assert on the value of the rhs.
532-
self.eval_operand(op, location, None)
526+
self.eval_operand(op, None)
533527
.and_then(|op| self.ecx.read_immediate(&op).ok())
534528
.map_or(DbgVal::Underscore, |op| DbgVal::Val(op.to_const_int()))
535529
};
@@ -586,44 +580,43 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
586580
return None;
587581
}
588582
use rustc_middle::mir::Rvalue::*;
589-
let layout = self.use_ecx(location, |this| this.ecx.eval_place(*dest))?.layout;
583+
let layout = self.use_ecx(|this| this.ecx.eval_place(*dest))?.layout;
590584
trace!(?layout);
591585

592586
let val: Value<'_> = match *rvalue {
593587
ThreadLocalRef(_) => return None,
594588

595-
Use(ref operand) => self.eval_operand(operand, location, Some(layout))?.into(),
589+
Use(ref operand) => self.eval_operand(operand, Some(layout))?.into(),
596590

597591
CopyForDeref(place) => self.eval_place(place, Some(layout))?.into(),
598592

599593
BinaryOp(bin_op, box (ref left, ref right)) => {
600594
let layout =
601595
rustc_const_eval::util::binop_left_homogeneous(bin_op).then_some(layout);
602-
let left = self.eval_operand(left, location, layout)?;
603-
let left = self.use_ecx(location, |this| this.ecx.read_immediate(&left))?;
596+
let left = self.eval_operand(left, layout)?;
597+
let left = self.use_ecx(|this| this.ecx.read_immediate(&left))?;
604598

605599
let layout =
606600
rustc_const_eval::util::binop_right_homogeneous(bin_op).then_some(left.layout);
607-
let right = self.eval_operand(right, location, layout)?;
608-
let right = self.use_ecx(location, |this| this.ecx.read_immediate(&right))?;
601+
let right = self.eval_operand(right, layout)?;
602+
let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
609603

610-
let val = self
611-
.use_ecx(location, |this| this.ecx.wrapping_binary_op(bin_op, &left, &right))?;
604+
let val =
605+
self.use_ecx(|this| this.ecx.wrapping_binary_op(bin_op, &left, &right))?;
612606
val.into()
613607
}
614608

615609
CheckedBinaryOp(bin_op, box (ref left, ref right)) => {
616-
let left = self.eval_operand(left, location, None)?;
617-
let left = self.use_ecx(location, |this| this.ecx.read_immediate(&left))?;
610+
let left = self.eval_operand(left, None)?;
611+
let left = self.use_ecx(|this| this.ecx.read_immediate(&left))?;
618612

619613
let layout =
620614
rustc_const_eval::util::binop_right_homogeneous(bin_op).then_some(left.layout);
621-
let right = self.eval_operand(right, location, layout)?;
622-
let right = self.use_ecx(location, |this| this.ecx.read_immediate(&right))?;
615+
let right = self.eval_operand(right, layout)?;
616+
let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
623617

624-
let (val, overflowed) = self.use_ecx(location, |this| {
625-
this.ecx.overflowing_binary_op(bin_op, &left, &right)
626-
})?;
618+
let (val, overflowed) =
619+
self.use_ecx(|this| this.ecx.overflowing_binary_op(bin_op, &left, &right))?;
627620
let overflowed = ImmTy::from_bool(overflowed, self.tcx);
628621
Value::Aggregate {
629622
variant: VariantIdx::new(0),
@@ -632,19 +625,18 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
632625
}
633626

634627
UnaryOp(un_op, ref operand) => {
635-
let operand = self.eval_operand(operand, location, Some(layout))?;
636-
let val = self.use_ecx(location, |this| this.ecx.read_immediate(&operand))?;
628+
let operand = self.eval_operand(operand, Some(layout))?;
629+
let val = self.use_ecx(|this| this.ecx.read_immediate(&operand))?;
637630

638-
let val = self.use_ecx(location, |this| this.ecx.wrapping_unary_op(un_op, &val))?;
631+
let val = self.use_ecx(|this| this.ecx.wrapping_unary_op(un_op, &val))?;
639632
val.into()
640633
}
641634

642635
Aggregate(ref kind, ref fields) => Value::Aggregate {
643636
fields: fields
644637
.iter()
645638
.map(|field| {
646-
self.eval_operand(field, location, None)
647-
.map_or(Value::Uninit, Value::Immediate)
639+
self.eval_operand(field, None).map_or(Value::Uninit, Value::Immediate)
648640
})
649641
.collect(),
650642
variant: match **kind {
@@ -676,7 +668,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
676668
Ref(..) | AddressOf(..) => return None,
677669

678670
NullaryOp(ref null_op, ty) => {
679-
let op_layout = self.use_ecx(location, |this| this.ecx.layout_of(ty))?;
671+
let op_layout = self.use_ecx(|this| this.ecx.layout_of(ty))?;
680672
let val = match null_op {
681673
NullOp::SizeOf => op_layout.size.bytes(),
682674
NullOp::AlignOf => op_layout.align.abi.bytes(),
@@ -691,21 +683,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
691683

692684
Cast(ref kind, ref value, to) => match kind {
693685
CastKind::IntToInt | CastKind::IntToFloat => {
694-
let value = self.eval_operand(value, location, None)?;
686+
let value = self.eval_operand(value, None)?;
695687
let value = self.ecx.read_immediate(&value).ok()?;
696688
let to = self.ecx.layout_of(to).ok()?;
697689
let res = self.ecx.int_to_int_or_float(&value, to).ok()?;
698690
res.into()
699691
}
700692
CastKind::FloatToFloat | CastKind::FloatToInt => {
701-
let value = self.eval_operand(value, location, None)?;
693+
let value = self.eval_operand(value, None)?;
702694
let value = self.ecx.read_immediate(&value).ok()?;
703695
let to = self.ecx.layout_of(to).ok()?;
704696
let res = self.ecx.float_to_float_or_int(&value, to).ok()?;
705697
res.into()
706698
}
707699
CastKind::Transmute => {
708-
let value = self.eval_operand(value, location, None)?;
700+
let value = self.eval_operand(value, None)?;
709701
let to = self.ecx.layout_of(to).ok()?;
710702
// `offset` for immediates only supports scalar/scalar-pair ABIs,
711703
// so bail out if the target is not one.
@@ -725,12 +717,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
725717
let variant = match self.get_const(place)? {
726718
Value::Immediate(op) => {
727719
let op = op.clone();
728-
self.use_ecx(location, |this| this.ecx.read_discriminant(&op))?
720+
self.use_ecx(|this| this.ecx.read_discriminant(&op))?
729721
}
730722
Value::Aggregate { variant, .. } => *variant,
731723
Value::Uninit => return None,
732724
};
733-
let imm = self.use_ecx(location, |this| {
725+
let imm = self.use_ecx(|this| {
734726
this.ecx.discriminant_for_variant(
735727
place.ty(this.local_decls(), this.tcx).ty,
736728
variant,
@@ -792,7 +784,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
792784
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
793785
trace!("visit_constant: {:?}", constant);
794786
self.super_constant(constant, location);
795-
self.eval_constant(constant, location, None);
787+
self.eval_constant(constant, None);
796788
}
797789

798790
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
@@ -865,9 +857,8 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
865857
self.check_assertion(*expected, msg, cond, location);
866858
}
867859
TerminatorKind::SwitchInt { ref discr, ref targets } => {
868-
if let Some(ref value) = self.eval_operand(discr, location, None)
869-
&& let Some(value_const) =
870-
self.use_ecx(location, |this| this.ecx.read_scalar(value))
860+
if let Some(ref value) = self.eval_operand(discr, None)
861+
&& let Some(value_const) = self.use_ecx(|this| this.ecx.read_scalar(value))
871862
&& let Ok(constant) = value_const.try_to_int()
872863
&& let Ok(constant) = constant.to_bits(constant.size())
873864
{

0 commit comments

Comments
 (0)