Rollup merge of #155730 - oli-obk:cleanups, r=petrochenkov

Some cleanups around per parent disambiguators

r? @petrochenkov

follow-up to rust-lang/rust#155547

The two remaining uses are

* resolve_bound_vars, where it is a reasonable way to do it instead of having another field in the visitor that needs to get scoped (set & reset) every time we visit an opaque type. May still change that at some point, but it's not really an issue
* `create_def` in the resolver: will get removed together with my other refactorings for `node_id_to_def_id` (making that per-owner)
This commit is contained in:
Jonathan Brouwer
2026-04-24 18:19:16 +02:00
committed by GitHub
2 changed files with 25 additions and 27 deletions
@@ -16,10 +16,7 @@
use hir::def::DefKind;
use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir::def_id::LocalDefIdMap;
use rustc_hir::definitions::{
DefPathData, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
};
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
use rustc_hir::{self as hir};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::mir::interpret::{
@@ -108,7 +105,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
ecx: &mut InterpCx<'tcx, M>,
alloc_id: AllocId,
mutability: Mutability,
disambiguators: Option<&mut LocalDefIdMap<PerParentDisambiguatorState>>,
disambiguator: Option<&mut PerParentDisambiguatorState>,
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
trace!("intern_shallow {:?}", alloc_id);
// remove allocation
@@ -132,7 +129,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
static_id,
alloc_id,
alloc,
disambiguators.expect("disambiguators needed"),
disambiguator.expect("disambiguator needed"),
);
} else {
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
@@ -147,19 +144,18 @@ fn intern_as_new_static<'tcx>(
static_id: LocalDefId,
alloc_id: AllocId,
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
// `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.
let feed = tcx.create_def(
static_id,
None,
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
Some(DefPathData::NestedStatic),
//FIXME(oli-obk): cleanup (https://github.com/rust-lang/rust/pull/155547#discussion_r3110792640)
disambiguators.get_or_create(static_id),
disambiguator,
);
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,
ret: &MPlaceTy<'tcx>,
) -> 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
// 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
// recursively in the loop below.
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`.
// But still change its mutability to match the requested one.
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
} 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`",
// 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
// `static mut`.
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);
}
if found_bad_mutable_ptr {
+11 -13
View File
@@ -1,8 +1,6 @@
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdMap};
use rustc_hir::definitions::{
DefPathData, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
};
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{self as hir, ConstItemRhs, ImplItemImplKind, ItemKind};
use rustc_middle::query::Providers;
@@ -131,7 +129,7 @@ struct RPITVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
synthetics: Vec<LocalDefId>,
data: DefPathData,
disambiguators: &'a mut LocalDefIdMap<PerParentDisambiguatorState>,
disambiguator: &'a mut PerParentDisambiguatorState,
}
impl<'tcx> Visitor<'tcx> for RPITVisitor<'_, 'tcx> {
@@ -140,7 +138,7 @@ fn visit_opaque_ty(&mut self, opaque: &'tcx hir::OpaqueTy<'tcx>) -> Self::Result
self.tcx,
opaque.def_id,
self.data,
&mut self.disambiguators,
&mut self.disambiguator,
));
intravisit::walk_opaque_ty(self, opaque)
}
@@ -151,7 +149,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
def_id: LocalDefId,
) -> DefIdMap<Vec<DefId>> {
let item = tcx.hir_expect_item(def_id);
let disambiguators = &mut Default::default();
let disambiguator = &mut PerParentDisambiguatorState::new(def_id);
match item.kind {
ItemKind::Trait(.., trait_item_refs) => trait_item_refs
.iter()
@@ -165,7 +163,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
};
let def_name = tcx.item_name(fn_def_id.to_def_id());
let data = DefPathData::AnonAssocTy(def_name);
let mut visitor = RPITVisitor { tcx, synthetics: vec![], data, disambiguators };
let mut visitor = RPITVisitor { tcx, synthetics: vec![], data, disambiguator };
visitor.visit_fn_ret_ty(output);
let defs = visitor
.synthetics
@@ -199,7 +197,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
return Some((did, vec![]));
};
let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| {
associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguators)
associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguator)
.to_def_id()
});
Some((did, iter.collect()))
@@ -223,7 +221,7 @@ fn associated_type_for_impl_trait_in_trait(
tcx: TyCtxt<'_>,
opaque_ty_def_id: LocalDefId,
data: DefPathData,
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>,
disambiguator: &mut PerParentDisambiguatorState,
) -> LocalDefId {
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. }) =
@@ -242,7 +240,7 @@ fn associated_type_for_impl_trait_in_trait(
None,
DefKind::AssocTy,
Some(data),
disambiguators.get_or_create(trait_def_id),
disambiguator,
);
let local_def_id = trait_assoc_ty.def_id();
@@ -285,7 +283,7 @@ fn associated_type_for_impl_trait_in_impl(
tcx: TyCtxt<'_>,
trait_assoc_def_id: DefId,
impl_fn: &hir::ImplItem<'_>,
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>,
disambiguator: &mut PerParentDisambiguatorState,
) -> LocalDefId {
let impl_local_def_id = tcx.local_parent(impl_fn.owner_id.def_id);
@@ -308,7 +306,7 @@ fn associated_type_for_impl_trait_in_impl(
None,
DefKind::AssocTy,
Some(data),
disambiguators.get_or_create(impl_local_def_id),
disambiguator,
);
let local_def_id = impl_assoc_ty.def_id();