Skip to content

Commit 8e00329

Browse files
authored
Rollup merge of #64419 - wesleywiser:const_prop_use_ecx, r=oli-obk
Deduplicate some code between miri and const prop r? @oli-obk
2 parents 1fb2f4a + 0b333ef commit 8e00329

22 files changed

+218
-221
lines changed

src/librustc/mir/interpret/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ pub enum UnsupportedOpInfo<'tcx> {
393393
InvalidPointerMath,
394394
ReadUndefBytes(Size),
395395
DeadLocal,
396+
UninitializedLocal,
396397
InvalidBoolOp(mir::BinOp),
397398
UnimplementedTraitSelection,
398399
CalledClosureAsFunction,
@@ -420,6 +421,7 @@ pub enum UnsupportedOpInfo<'tcx> {
420421
HeapAllocNonPowerOfTwoAlignment(u64),
421422
ReadFromReturnPointer,
422423
PathNotFound(Vec<String>),
424+
ReadOfStaticInConst,
423425
}
424426

425427
impl fmt::Debug for UnsupportedOpInfo<'tcx> {
@@ -491,6 +493,8 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
491493
addresses, e.g., comparing pointers into different allocations"),
492494
DeadLocal =>
493495
write!(f, "tried to access a dead local variable"),
496+
UninitializedLocal =>
497+
write!(f, "tried to access an uninitialized local variable"),
494498
DerefFunctionPointer =>
495499
write!(f, "tried to dereference a function pointer"),
496500
ExecuteMemory =>
@@ -532,6 +536,8 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
532536
not a power of two"),
533537
Unsupported(ref msg) =>
534538
write!(f, "{}", msg),
539+
ReadOfStaticInConst =>
540+
write!(f, "tried to read from a static during const evaluation"),
535541
}
536542
}
537543
}

src/librustc_mir/interpret/eval_context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ impl<'tcx, Tag: Copy + 'static> LocalState<'tcx, Tag> {
136136
match self.value {
137137
LocalValue::Dead => throw_unsup!(DeadLocal),
138138
LocalValue::Uninitialized =>
139-
bug!("The type checker should prevent reading from a never-written local"),
139+
// this is reachable from ConstProp
140+
throw_unsup!(UninitializedLocal),
140141
LocalValue::Live(val) => Ok(val),
141142
}
142143
}

src/librustc_mir/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
470470

471471
// Evaluate a place with the goal of reading from it. This lets us sometimes
472472
// avoid allocations.
473-
pub(super) fn eval_place_to_op(
473+
pub fn eval_place_to_op(
474474
&self,
475475
place: &mir::Place<'tcx>,
476476
layout: Option<TyLayout<'tcx>>,

src/librustc_mir/interpret/place.rs

+8
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,14 @@ where
595595
}
596596

597597
StaticKind::Static => {
598+
//if the first frame on the stack isn't a static item, then we shouldn't
599+
//eval any static places
600+
if let ty::InstanceDef::Item(item_def_id) = self.stack[0].instance.def {
601+
if !self.tcx.is_static(item_def_id) {
602+
trace!("eval_static_to_mplace: can't eval static in constant");
603+
throw_unsup!(ReadOfStaticInConst);
604+
}
605+
}
598606
let ty = place_static.ty;
599607
assert!(!ty.needs_subst());
600608
let layout = self.layout_of(ty)?;

src/librustc_mir/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
132132
///
133133
/// There is no separate `eval_rvalue` function. Instead, the code for handling each rvalue
134134
/// type writes its results directly into the memory specified by the place.
135-
fn eval_rvalue_into_place(
135+
pub fn eval_rvalue_into_place(
136136
&mut self,
137137
rvalue: &mir::Rvalue<'tcx>,
138138
place: &mir::Place<'tcx>,

0 commit comments

Comments
 (0)