Skip to content

Commit 7a73b87

Browse files
committed
fix const_prop ICE
1 parent 58a56cc commit 7a73b87

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

src/librustc_mir/const_eval/error.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ impl fmt::Display for ConstEvalErrKind {
3434
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
3535
}
3636
ConstAccessesStatic => write!(f, "constant accesses static"),
37-
ModifiedGlobal => write!(
38-
f,
39-
"modifying a static's initial value from another static's \
40-
initializer"
41-
),
37+
ModifiedGlobal => {
38+
write!(f, "modifying a static's initial value from another static's initializer")
39+
}
4240
AssertFailure(ref msg) => write!(f, "{:?}", msg),
4341
Panic { msg, line, col, file } => {
4442
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)

src/librustc_mir/const_eval/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
358358
} else if is_write {
359359
Err(ConstEvalErrKind::ModifiedGlobal.into())
360360
} else if memory_extra.can_access_statics || def_id.is_none() {
361+
// `def_id.is_none()` indicates this is not a static, but a const or so.
361362
Ok(())
362363
} else {
363364
Err(ConstEvalErrKind::ConstAccessesStatic.into())

src/librustc_mir/interpret/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
209209
}
210210

211211
/// Called before a global allocation is accessed.
212+
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
212213
#[inline]
213214
fn before_access_global(
214215
_memory_extra: &Self::MemoryExtra,

src/librustc_mir/interpret/memory.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
416416
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
417417
let alloc = tcx.alloc_map.lock().get(id);
418418
let (alloc, def_id) = match alloc {
419-
Some(GlobalAlloc::Memory(mem)) => (mem, None),
419+
Some(GlobalAlloc::Memory(mem)) => {
420+
// Memory of a constant or promoted or anonymous memory referenced by a static.
421+
(mem, None)
422+
}
420423
Some(GlobalAlloc::Function(..)) => throw_ub!(DerefFunctionPointer(id)),
421424
None => throw_ub!(PointerUseAfterFree(id)),
422425
Some(GlobalAlloc::Static(def_id)) => {

src/librustc_mir/transform/const_prop.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,19 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
274274
_memory_extra: &(),
275275
_alloc_id: AllocId,
276276
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
277-
_def_id: Option<DefId>,
277+
def_id: Option<DefId>,
278278
is_write: bool,
279279
) -> InterpResult<'tcx> {
280280
if is_write {
281281
throw_machine_stop_str!("can't write to global");
282282
}
283283
// If the static allocation is mutable or if it has relocations (it may be legal to mutate
284284
// the memory behind that in the future), then we can't const prop it.
285-
if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
285+
// FIXME: we only check statics here (that have a `DefId`), not other mutable allocations.
286+
// Why that?
287+
if def_id.is_some()
288+
&& (allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0)
289+
{
286290
throw_machine_stop_str!("can't eval mutable statics in ConstProp");
287291
}
288292

0 commit comments

Comments
 (0)