Skip to content

Commit

Permalink
fix const_prop ICE
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 24, 2020
1 parent 58a56cc commit 7a73b87
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/librustc_mir/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ impl fmt::Display for ConstEvalErrKind {
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
}
ConstAccessesStatic => write!(f, "constant accesses static"),
ModifiedGlobal => write!(
f,
"modifying a static's initial value from another static's \
initializer"
),
ModifiedGlobal => {
write!(f, "modifying a static's initial value from another static's initializer")
}
AssertFailure(ref msg) => write!(f, "{:?}", msg),
Panic { msg, line, col, file } => {
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
} else if is_write {
Err(ConstEvalErrKind::ModifiedGlobal.into())
} else if memory_extra.can_access_statics || def_id.is_none() {
// `def_id.is_none()` indicates this is not a static, but a const or so.
Ok(())
} else {
Err(ConstEvalErrKind::ConstAccessesStatic.into())
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
}

/// Called before a global allocation is accessed.
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
#[inline]
fn before_access_global(
_memory_extra: &Self::MemoryExtra,
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
let alloc = tcx.alloc_map.lock().get(id);
let (alloc, def_id) = match alloc {
Some(GlobalAlloc::Memory(mem)) => (mem, None),
Some(GlobalAlloc::Memory(mem)) => {
// Memory of a constant or promoted or anonymous memory referenced by a static.
(mem, None)
}
Some(GlobalAlloc::Function(..)) => throw_ub!(DerefFunctionPointer(id)),
None => throw_ub!(PointerUseAfterFree(id)),
Some(GlobalAlloc::Static(def_id)) => {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,19 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
_memory_extra: &(),
_alloc_id: AllocId,
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
_def_id: Option<DefId>,
def_id: Option<DefId>,
is_write: bool,
) -> InterpResult<'tcx> {
if is_write {
throw_machine_stop_str!("can't write to global");
}
// If the static allocation is mutable or if it has relocations (it may be legal to mutate
// the memory behind that in the future), then we can't const prop it.
if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
// FIXME: we only check statics here (that have a `DefId`), not other mutable allocations.
// Why that?
if def_id.is_some()
&& (allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0)
{
throw_machine_stop_str!("can't eval mutable statics in ConstProp");
}

Expand Down

0 comments on commit 7a73b87

Please sign in to comment.