Skip to content

Commit d5a3b9c

Browse files
committed
Move DisambiguatorState into intern_const_alloc_recursive
1 parent 4869e20 commit d5a3b9c

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

compiler/rustc_const_eval/src/const_eval/dummy_machine.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc_hir::def_id::LocalDefId;
2-
use rustc_hir::definitions::DisambiguatorState;
31
use rustc_middle::mir::interpret::{AllocId, ConstAllocation, InterpResult};
42
use rustc_middle::mir::*;
53
use rustc_middle::query::TyCtxtAt;
@@ -44,7 +42,7 @@ pub macro throw_machine_stop_str($($tt:tt)*) {{
4442
pub struct DummyMachine;
4543

4644
impl HasStaticRootDefId for DummyMachine {
47-
fn static_parent_and_disambiguator(&mut self) -> Option<(LocalDefId, &mut DisambiguatorState)> {
45+
fn static_def_id(&self) -> Option<rustc_hir::def_id::LocalDefId> {
4846
None
4947
}
5048
}

compiler/rustc_const_eval/src/const_eval/machine.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_abi::{Align, Size};
66
use rustc_ast::Mutability;
77
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry};
88
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_hir::definitions::DisambiguatorState;
109
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem};
1110
use rustc_middle::mir::AssertMessage;
1211
use rustc_middle::mir::interpret::ReportedErrorInfo;
@@ -64,7 +63,7 @@ pub struct CompileTimeMachine<'tcx> {
6463
/// If `Some`, we are evaluating the initializer of the static with the given `LocalDefId`,
6564
/// storing the result in the given `AllocId`.
6665
/// Used to prevent reads from a static's base allocation, as that may allow for self-initialization loops.
67-
pub(crate) static_root_ids: Option<(AllocId, LocalDefId, DisambiguatorState)>,
66+
pub(crate) static_root_ids: Option<(AllocId, LocalDefId)>,
6867

6968
/// A cache of "data range" computations for unions (i.e., the offsets of non-padding bytes).
7069
union_data_ranges: FxHashMap<Ty<'tcx>, RangeSet>,
@@ -707,7 +706,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
707706

708707
fn before_alloc_read(ecx: &InterpCx<'tcx, Self>, alloc_id: AllocId) -> InterpResult<'tcx> {
709708
// Check if this is the currently evaluated static.
710-
if Some(alloc_id) == ecx.machine.static_root_ids.as_ref().map(|(id, ..)| *id) {
709+
if Some(alloc_id) == ecx.machine.static_root_ids.map(|(id, _)| id) {
711710
return Err(ConstEvalErrKind::RecursiveStatic).into();
712711
}
713712
// If this is another static, make sure we fire off the query to detect cycles.

compiler/rustc_const_eval/src/interpret/intern.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ pub trait CompileTimeMachine<'tcx, T> = Machine<
4646
pub trait HasStaticRootDefId {
4747
/// Returns the `DefId` of the static item that is currently being evaluated.
4848
/// Used for interning to be able to handle nested allocations.
49-
fn static_parent_and_disambiguator(&mut self) -> Option<(LocalDefId, &mut DisambiguatorState)>;
49+
fn static_def_id(&self) -> Option<LocalDefId>;
5050
}
5151

5252
impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
53-
fn static_parent_and_disambiguator(&mut self) -> Option<(LocalDefId, &mut DisambiguatorState)> {
54-
let (_, static_id, d) = self.static_root_ids.as_mut()?;
55-
Some((*static_id, d))
53+
fn static_def_id(&self) -> Option<LocalDefId> {
54+
Some(self.static_root_ids?.1)
5655
}
5756
}
5857

@@ -67,6 +66,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
6766
ecx: &mut InterpCx<'tcx, M>,
6867
alloc_id: AllocId,
6968
mutability: Mutability,
69+
disambiguator: Option<&mut DisambiguatorState>,
7070
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, ()> {
7171
trace!("intern_shallow {:?}", alloc_id);
7272
// remove allocation
@@ -88,8 +88,14 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
8888
}
8989
// link the alloc id to the actual allocation
9090
let alloc = ecx.tcx.mk_const_alloc(alloc);
91-
if let Some((static_id, disambiguator)) = ecx.machine.static_parent_and_disambiguator() {
92-
intern_as_new_static(ecx.tcx, static_id, alloc_id, alloc, disambiguator);
91+
if let Some(static_id) = ecx.machine.static_def_id() {
92+
intern_as_new_static(
93+
ecx.tcx,
94+
static_id,
95+
alloc_id,
96+
alloc,
97+
disambiguator.expect("disambiguator needed"),
98+
);
9399
} else {
94100
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
95101
}
@@ -105,6 +111,10 @@ fn intern_as_new_static<'tcx>(
105111
alloc: ConstAllocation<'tcx>,
106112
disambiguator: &mut DisambiguatorState,
107113
) {
114+
// `intern_const_alloc_recursive` is called once per static and it contains the `DisambiguatorState`.
115+
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
116+
// `DisambiguatorState` ensures the generated path is unique for this call as we generate
117+
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
108118
let feed = tcx.create_def(
109119
static_id,
110120
None,
@@ -158,6 +168,8 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
158168
intern_kind: InternKind,
159169
ret: &MPlaceTy<'tcx>,
160170
) -> Result<(), InternResult> {
171+
let mut disambiguator = DisambiguatorState::new();
172+
161173
// We are interning recursively, and for mutability we are distinguishing the "root" allocation
162174
// that we are starting in, and all other allocations that we are encountering recursively.
163175
let (base_mutability, inner_mutability, is_static) = match intern_kind {
@@ -201,7 +213,9 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
201213
alloc.1.mutability = base_mutability;
202214
alloc.1.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
203215
} else {
204-
intern_shallow(ecx, base_alloc_id, base_mutability).unwrap().collect()
216+
intern_shallow(ecx, base_alloc_id, base_mutability, Some(&mut disambiguator))
217+
.unwrap()
218+
.collect()
205219
};
206220
// We need to distinguish "has just been interned" from "was already in `tcx`",
207221
// so we track this in a separate set.
@@ -295,7 +309,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
295309
// okay with losing some potential for immutability here. This can anyway only affect
296310
// `static mut`.
297311
just_interned.insert(alloc_id);
298-
match intern_shallow(ecx, alloc_id, inner_mutability) {
312+
match intern_shallow(ecx, alloc_id, inner_mutability, Some(&mut disambiguator)) {
299313
Ok(nested) => todo.extend(nested),
300314
Err(()) => {
301315
ecx.tcx.dcx().delayed_bug("found dangling pointer during const interning");
@@ -317,8 +331,9 @@ pub fn intern_const_alloc_for_constprop<'tcx, T, M: CompileTimeMachine<'tcx, T>>
317331
return interp_ok(());
318332
}
319333
// Move allocation to `tcx`.
320-
if let Some(_) =
321-
(intern_shallow(ecx, alloc_id, Mutability::Not).map_err(|()| err_ub!(DeadLocal))?).next()
334+
if let Some(_) = (intern_shallow(ecx, alloc_id, Mutability::Not, None)
335+
.map_err(|()| err_ub!(DeadLocal))?)
336+
.next()
322337
{
323338
// We are not doing recursive interning, so we don't currently support provenance.
324339
// (If this assertion ever triggers, we should just implement a
@@ -344,7 +359,7 @@ impl<'tcx> InterpCx<'tcx, DummyMachine> {
344359
let dest = self.allocate(layout, MemoryKind::Stack)?;
345360
f(self, &dest.clone().into())?;
346361
let alloc_id = dest.ptr().provenance.unwrap().alloc_id(); // this was just allocated, it must have provenance
347-
for prov in intern_shallow(self, alloc_id, Mutability::Not).unwrap() {
362+
for prov in intern_shallow(self, alloc_id, Mutability::Not, None).unwrap() {
348363
// We are not doing recursive interning, so we don't currently support provenance.
349364
// (If this assertion ever triggers, we should just implement a
350365
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.

compiler/rustc_const_eval/src/interpret/util.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_hir::def_id::LocalDefId;
2-
use rustc_hir::definitions::DisambiguatorState;
32
use rustc_middle::mir;
43
use rustc_middle::mir::interpret::{AllocInit, Allocation, InterpResult, Pointer};
54
use rustc_middle::ty::layout::TyAndLayout;
@@ -41,8 +40,8 @@ pub(crate) fn create_static_alloc<'tcx>(
4140
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
4241
let alloc = Allocation::try_new(layout.size, layout.align.abi, AllocInit::Uninit)?;
4342
let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
44-
assert!(ecx.machine.static_root_ids.is_none());
45-
ecx.machine.static_root_ids = Some((alloc_id, static_def_id, DisambiguatorState::new()));
43+
assert_eq!(ecx.machine.static_root_ids, None);
44+
ecx.machine.static_root_ids = Some((alloc_id, static_def_id));
4645
assert!(ecx.memory.alloc_map.insert(alloc_id, (MemoryKind::Stack, alloc)).is_none());
4746
interp_ok(ecx.ptr_to_mplace(Pointer::from(alloc_id).into(), layout))
4847
}

0 commit comments

Comments
 (0)