Do not allocate a second "background" alloc id for the main allocation of a static.
Instead we re-use the static's alloc id within the interpreter for its initializer to refer to the `Allocation` that only exists within the interpreter.
This commit is contained in:
@@ -58,6 +58,9 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
|
||||
|
||||
/// Whether to check alignment during evaluation.
|
||||
pub(super) check_alignment: CheckAlignment,
|
||||
|
||||
/// Used to prevent reads from a static's base allocation, as that may allow for self-initialization.
|
||||
pub(crate) static_root_alloc_id: Option<AllocId>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@@ -91,6 +94,7 @@ impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
|
||||
stack: Vec::new(),
|
||||
can_access_mut_global,
|
||||
check_alignment,
|
||||
static_root_alloc_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -746,6 +750,17 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
||||
// Everything else is fine.
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn before_alloc_read(
|
||||
ecx: &InterpCx<'mir, 'tcx, Self>,
|
||||
alloc_id: AllocId,
|
||||
) -> InterpResult<'tcx> {
|
||||
if Some(alloc_id) == ecx.machine.static_root_alloc_id {
|
||||
Err(ConstEvalErrKind::RecursiveStatic.into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Please do not add any code below the above `Machine` trait impl. I (oli-obk) plan more cleanups
|
||||
|
||||
Reference in New Issue
Block a user