All nested statics in a single interning run have the same parent

So we do not need to disambiguate considering parents
This commit is contained in:
Oli Scherer
2026-04-21 12:44:13 +02:00
parent 92c7010294
commit e90878b9f2
@@ -16,10 +16,7 @@
use hir::def::DefKind; use hir::def::DefKind;
use rustc_ast::Mutability; use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir::def_id::LocalDefIdMap; use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
use rustc_hir::definitions::{
DefPathData, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
};
use rustc_hir::{self as hir}; use rustc_hir::{self as hir};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::mir::interpret::{ use rustc_middle::mir::interpret::{
@@ -108,7 +105,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
ecx: &mut InterpCx<'tcx, M>, ecx: &mut InterpCx<'tcx, M>,
alloc_id: AllocId, alloc_id: AllocId,
mutability: Mutability, mutability: Mutability,
disambiguators: Option<&mut LocalDefIdMap<PerParentDisambiguatorState>>, disambiguator: Option<&mut PerParentDisambiguatorState>,
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> { ) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
trace!("intern_shallow {:?}", alloc_id); trace!("intern_shallow {:?}", alloc_id);
// remove allocation // remove allocation
@@ -132,7 +129,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
static_id, static_id,
alloc_id, alloc_id,
alloc, alloc,
disambiguators.expect("disambiguators needed"), disambiguator.expect("disambiguator needed"),
); );
} else { } else {
ecx.tcx.set_alloc_id_memory(alloc_id, alloc); ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
@@ -147,19 +144,18 @@ fn intern_as_new_static<'tcx>(
static_id: LocalDefId, static_id: LocalDefId,
alloc_id: AllocId, alloc_id: AllocId,
alloc: ConstAllocation<'tcx>, alloc: ConstAllocation<'tcx>,
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>, disambiguator: &mut PerParentDisambiguatorState,
) { ) {
// `intern_const_alloc_recursive` is called once per static and it contains the `DisambiguatorState`. // `intern_const_alloc_recursive` is called once per static and it contains the `PerParentDisambiguatorState`.
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the // The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
// `DisambiguatorState` ensures the generated path is unique for this call as we generate // `PerParentDisambiguatorState` ensures the generated path is unique for this call as we generate
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call. // `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
let feed = tcx.create_def( let feed = tcx.create_def(
static_id, static_id,
None, None,
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true }, DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
Some(DefPathData::NestedStatic), Some(DefPathData::NestedStatic),
//FIXME(oli-obk): cleanup (https://github.com/rust-lang/rust/pull/155547#discussion_r3110792640) disambiguator,
disambiguators.get_or_create(static_id),
); );
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id()); tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
@@ -209,7 +205,9 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
intern_kind: InternKind, intern_kind: InternKind,
ret: &MPlaceTy<'tcx>, ret: &MPlaceTy<'tcx>,
) -> Result<(), InternError> { ) -> Result<(), InternError> {
let mut disambiguators = Default::default(); let mut disambiguator =
ecx.machine.static_def_id().map(|id| PerParentDisambiguatorState::new(id));
let mut disambiguator = disambiguator.as_mut();
// We are interning recursively, and for mutability we are distinguishing the "root" allocation // We are interning recursively, and for mutability we are distinguishing the "root" allocation
// that we are starting in, and all other allocations that we are encountering recursively. // that we are starting in, and all other allocations that we are encountering recursively.
@@ -248,13 +246,15 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
// This gives us the initial set of nested allocations, which will then all be processed // This gives us the initial set of nested allocations, which will then all be processed
// recursively in the loop below. // recursively in the loop below.
let mut todo: Vec<_> = if is_static { let mut todo: Vec<_> = if is_static {
assert!(disambiguator.is_some());
// Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`. // Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`.
// But still change its mutability to match the requested one. // But still change its mutability to match the requested one.
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap(); let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?; prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect() alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
} else { } else {
intern_shallow(ecx, base_alloc_id, base_mutability, Some(&mut disambiguators))?.collect() assert!(disambiguator.is_none());
intern_shallow(ecx, base_alloc_id, base_mutability, None)?.collect()
}; };
// We need to distinguish "has just been interned" from "was already in `tcx`", // We need to distinguish "has just been interned" from "was already in `tcx`",
// so we track this in a separate set. // so we track this in a separate set.
@@ -336,7 +336,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
// okay with losing some potential for immutability here. This can anyway only affect // okay with losing some potential for immutability here. This can anyway only affect
// `static mut`. // `static mut`.
just_interned.insert(alloc_id); just_interned.insert(alloc_id);
let next = intern_shallow(ecx, alloc_id, inner_mutability, Some(&mut disambiguators))?; let next = intern_shallow(ecx, alloc_id, inner_mutability, disambiguator.as_deref_mut())?;
todo.extend(next); todo.extend(next);
} }
if found_bad_mutable_ptr { if found_bad_mutable_ptr {