Skip to content

Commit bbdfe21

Browse files
Rollup merge of rust-lang#155730 - oli-obk:cleanups, r=petrochenkov
Some cleanups around per parent disambiguators r? @petrochenkov follow-up to rust-lang#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)
2 parents ef162a5 + bfb085d commit bbdfe21

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
use hir::def::DefKind;
1717
use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
19-
use rustc_hir::def_id::LocalDefIdMap;
20-
use rustc_hir::definitions::{
21-
DefPathData, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
22-
};
19+
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
2320
use rustc_hir::{self as hir};
2421
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2522
use rustc_middle::mir::interpret::{
@@ -108,7 +105,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
108105
ecx: &mut InterpCx<'tcx, M>,
109106
alloc_id: AllocId,
110107
mutability: Mutability,
111-
disambiguators: Option<&mut LocalDefIdMap<PerParentDisambiguatorState>>,
108+
disambiguator: Option<&mut PerParentDisambiguatorState>,
112109
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
113110
trace!("intern_shallow {:?}", alloc_id);
114111
// remove allocation
@@ -132,7 +129,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
132129
static_id,
133130
alloc_id,
134131
alloc,
135-
disambiguators.expect("disambiguators needed"),
132+
disambiguator.expect("disambiguator needed"),
136133
);
137134
} else {
138135
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
@@ -147,19 +144,18 @@ fn intern_as_new_static<'tcx>(
147144
static_id: LocalDefId,
148145
alloc_id: AllocId,
149146
alloc: ConstAllocation<'tcx>,
150-
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>,
147+
disambiguator: &mut PerParentDisambiguatorState,
151148
) {
152-
// `intern_const_alloc_recursive` is called once per static and it contains the `DisambiguatorState`.
149+
// `intern_const_alloc_recursive` is called once per static and it contains the `PerParentDisambiguatorState`.
153150
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
154-
// `DisambiguatorState` ensures the generated path is unique for this call as we generate
151+
// `PerParentDisambiguatorState` ensures the generated path is unique for this call as we generate
155152
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
156153
let feed = tcx.create_def(
157154
static_id,
158155
None,
159156
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
160157
Some(DefPathData::NestedStatic),
161-
//FIXME(oli-obk): cleanup (https://github.com/rust-lang/rust/pull/155547#discussion_r3110792640)
162-
disambiguators.get_or_create(static_id),
158+
disambiguator,
163159
);
164160
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
165161

@@ -209,7 +205,9 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
209205
intern_kind: InternKind,
210206
ret: &MPlaceTy<'tcx>,
211207
) -> Result<(), InternError> {
212-
let mut disambiguators = Default::default();
208+
let mut disambiguator =
209+
ecx.machine.static_def_id().map(|id| PerParentDisambiguatorState::new(id));
210+
let mut disambiguator = disambiguator.as_mut();
213211

214212
// We are interning recursively, and for mutability we are distinguishing the "root" allocation
215213
// 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>>(
248246
// This gives us the initial set of nested allocations, which will then all be processed
249247
// recursively in the loop below.
250248
let mut todo: Vec<_> = if is_static {
249+
assert!(disambiguator.is_some());
251250
// Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`.
252251
// But still change its mutability to match the requested one.
253252
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
254253
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
255254
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
256255
} else {
257-
intern_shallow(ecx, base_alloc_id, base_mutability, Some(&mut disambiguators))?.collect()
256+
assert!(disambiguator.is_none());
257+
intern_shallow(ecx, base_alloc_id, base_mutability, None)?.collect()
258258
};
259259
// We need to distinguish "has just been interned" from "was already in `tcx`",
260260
// so we track this in a separate set.
@@ -336,7 +336,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
336336
// okay with losing some potential for immutability here. This can anyway only affect
337337
// `static mut`.
338338
just_interned.insert(alloc_id);
339-
let next = intern_shallow(ecx, alloc_id, inner_mutability, Some(&mut disambiguators))?;
339+
let next = intern_shallow(ecx, alloc_id, inner_mutability, disambiguator.as_deref_mut())?;
340340
todo.extend(next);
341341
}
342342
if found_bad_mutable_ptr {

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use rustc_hir::def::DefKind;
2-
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdMap};
3-
use rustc_hir::definitions::{
4-
DefPathData, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
5-
};
2+
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
3+
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
64
use rustc_hir::intravisit::{self, Visitor};
75
use rustc_hir::{self as hir, ConstItemRhs, ImplItemImplKind, ItemKind};
86
use rustc_middle::query::Providers;
@@ -131,7 +129,7 @@ struct RPITVisitor<'a, 'tcx> {
131129
tcx: TyCtxt<'tcx>,
132130
synthetics: Vec<LocalDefId>,
133131
data: DefPathData,
134-
disambiguators: &'a mut LocalDefIdMap<PerParentDisambiguatorState>,
132+
disambiguator: &'a mut PerParentDisambiguatorState,
135133
}
136134

137135
impl<'tcx> Visitor<'tcx> for RPITVisitor<'_, 'tcx> {
@@ -140,7 +138,7 @@ impl<'tcx> Visitor<'tcx> for RPITVisitor<'_, 'tcx> {
140138
self.tcx,
141139
opaque.def_id,
142140
self.data,
143-
&mut self.disambiguators,
141+
&mut self.disambiguator,
144142
));
145143
intravisit::walk_opaque_ty(self, opaque)
146144
}
@@ -151,7 +149,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
151149
def_id: LocalDefId,
152150
) -> DefIdMap<Vec<DefId>> {
153151
let item = tcx.hir_expect_item(def_id);
154-
let disambiguators = &mut Default::default();
152+
let disambiguator = &mut PerParentDisambiguatorState::new(def_id);
155153
match item.kind {
156154
ItemKind::Trait(.., trait_item_refs) => trait_item_refs
157155
.iter()
@@ -165,7 +163,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
165163
};
166164
let def_name = tcx.item_name(fn_def_id.to_def_id());
167165
let data = DefPathData::AnonAssocTy(def_name);
168-
let mut visitor = RPITVisitor { tcx, synthetics: vec![], data, disambiguators };
166+
let mut visitor = RPITVisitor { tcx, synthetics: vec![], data, disambiguator };
169167
visitor.visit_fn_ret_ty(output);
170168
let defs = visitor
171169
.synthetics
@@ -199,7 +197,7 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
199197
return Some((did, vec![]));
200198
};
201199
let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| {
202-
associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguators)
200+
associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguator)
203201
.to_def_id()
204202
});
205203
Some((did, iter.collect()))
@@ -223,7 +221,7 @@ fn associated_type_for_impl_trait_in_trait(
223221
tcx: TyCtxt<'_>,
224222
opaque_ty_def_id: LocalDefId,
225223
data: DefPathData,
226-
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>,
224+
disambiguator: &mut PerParentDisambiguatorState,
227225
) -> LocalDefId {
228226
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
229227
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. }) =
@@ -242,7 +240,7 @@ fn associated_type_for_impl_trait_in_trait(
242240
None,
243241
DefKind::AssocTy,
244242
Some(data),
245-
disambiguators.get_or_create(trait_def_id),
243+
disambiguator,
246244
);
247245

248246
let local_def_id = trait_assoc_ty.def_id();
@@ -285,7 +283,7 @@ fn associated_type_for_impl_trait_in_impl(
285283
tcx: TyCtxt<'_>,
286284
trait_assoc_def_id: DefId,
287285
impl_fn: &hir::ImplItem<'_>,
288-
disambiguators: &mut LocalDefIdMap<PerParentDisambiguatorState>,
286+
disambiguator: &mut PerParentDisambiguatorState,
289287
) -> LocalDefId {
290288
let impl_local_def_id = tcx.local_parent(impl_fn.owner_id.def_id);
291289

@@ -308,7 +306,7 @@ fn associated_type_for_impl_trait_in_impl(
308306
None,
309307
DefKind::AssocTy,
310308
Some(data),
311-
disambiguators.get_or_create(impl_local_def_id),
309+
disambiguator,
312310
);
313311

314312
let local_def_id = impl_assoc_ty.def_id();

0 commit comments

Comments
 (0)