Skip to content

Commit 90a80ee

Browse files
committed
Make disambiguator state global again.
1 parent 94321f7 commit 90a80ee

8 files changed

Lines changed: 34 additions & 97 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
5151
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
5252
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
54-
use rustc_hir::definitions::PerParentDisambiguatorState;
5554
use rustc_hir::lints::{AttributeLint, DelayedLint, DynAttribute};
5655
use rustc_hir::{
5756
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
@@ -94,7 +93,6 @@ pub mod stability;
9493
struct LoweringContext<'a, 'hir> {
9594
tcx: TyCtxt<'hir>,
9695
resolver: &'a ResolverAstLowering<'hir>,
97-
current_disambiguator: PerParentDisambiguatorState,
9896

9997
/// Used to allocate HIR nodes.
10098
arena: &'hir hir::Arena<'hir>,
@@ -169,7 +167,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
169167
Self {
170168
tcx,
171169
resolver,
172-
current_disambiguator: Default::default(),
173170
arena: tcx.hir_arena,
174171

175172
// HirId handling.
@@ -641,11 +638,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
641638
self.tcx.hir_def_key(self.local_def_id(node_id)),
642639
);
643640

644-
let def_id = self
645-
.tcx
646-
.at(span)
647-
.create_def(parent, name, def_kind, None, &mut self.current_disambiguator)
648-
.def_id();
641+
let def_id = self.tcx.at(span).create_def(parent, name, def_kind, None).def_id();
649642

650643
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
651644
self.node_id_to_def_id.insert(node_id, def_id);
@@ -697,16 +690,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
697690
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
698691
) {
699692
let owner_id = self.owner_id(owner);
700-
let def_id = owner_id.def_id;
701693

702-
let new_disambig = self
703-
.resolver
704-
.disambiguators
705-
.get(&def_id)
706-
.map(|s| s.steal())
707-
.unwrap_or_else(|| PerParentDisambiguatorState::new(def_id));
708-
709-
let disambiguator = std::mem::replace(&mut self.current_disambiguator, new_disambig);
710694
let current_attrs = std::mem::take(&mut self.attrs);
711695
let current_bodies = std::mem::take(&mut self.bodies);
712696
let current_define_opaque = std::mem::take(&mut self.define_opaque);
@@ -741,7 +725,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
741725
assert!(self.impl_trait_bounds.is_empty());
742726
let info = self.make_owner_info(item);
743727

744-
self.current_disambiguator = disambiguator;
745728
self.attrs = current_attrs;
746729
self.bodies = current_bodies;
747730
self.define_opaque = current_define_opaque;

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use hir::def::DefKind;
1717
use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
19-
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
19+
use rustc_hir::definitions::DefPathData;
2020
use rustc_hir::{self as hir};
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2222
use rustc_middle::mir::interpret::{
@@ -105,7 +105,6 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
105105
ecx: &mut InterpCx<'tcx, M>,
106106
alloc_id: AllocId,
107107
mutability: Mutability,
108-
disambiguator: Option<&mut PerParentDisambiguatorState>,
109108
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
110109
trace!("intern_shallow {:?}", alloc_id);
111110
// remove allocation
@@ -124,13 +123,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
124123
// link the alloc id to the actual allocation
125124
let alloc = ecx.tcx.mk_const_alloc(alloc);
126125
if let Some(static_id) = ecx.machine.static_def_id() {
127-
intern_as_new_static(
128-
ecx.tcx,
129-
static_id,
130-
alloc_id,
131-
alloc,
132-
disambiguator.expect("disambiguator needed"),
133-
);
126+
intern_as_new_static(ecx.tcx, static_id, alloc_id, alloc);
134127
} else {
135128
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
136129
}
@@ -144,18 +137,12 @@ fn intern_as_new_static<'tcx>(
144137
static_id: LocalDefId,
145138
alloc_id: AllocId,
146139
alloc: ConstAllocation<'tcx>,
147-
disambiguator: &mut PerParentDisambiguatorState,
148140
) {
149-
// `intern_const_alloc_recursive` is called once per static and it contains the `PerParentDisambiguatorState`.
150-
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
151-
// `PerParentDisambiguatorState` ensures the generated path is unique for this call as we generate
152-
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
153141
let feed = tcx.create_def(
154142
static_id,
155143
None,
156144
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
157145
Some(DefPathData::NestedStatic),
158-
disambiguator,
159146
);
160147
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
161148

@@ -205,10 +192,6 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
205192
intern_kind: InternKind,
206193
ret: &MPlaceTy<'tcx>,
207194
) -> Result<(), InternError> {
208-
let mut disambiguator =
209-
ecx.machine.static_def_id().map(|id| PerParentDisambiguatorState::new(id));
210-
let mut disambiguator = disambiguator.as_mut();
211-
212195
// We are interning recursively, and for mutability we are distinguishing the "root" allocation
213196
// that we are starting in, and all other allocations that we are encountering recursively.
214197
let (base_mutability, inner_mutability, is_static) = match intern_kind {
@@ -246,15 +229,13 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
246229
// This gives us the initial set of nested allocations, which will then all be processed
247230
// recursively in the loop below.
248231
let mut todo: Vec<_> = if is_static {
249-
assert!(disambiguator.is_some());
250232
// Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`.
251233
// But still change its mutability to match the requested one.
252234
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
253235
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
254236
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
255237
} else {
256-
assert!(disambiguator.is_none());
257-
intern_shallow(ecx, base_alloc_id, base_mutability, None)?.collect()
238+
intern_shallow(ecx, base_alloc_id, base_mutability)?.collect()
258239
};
259240
// We need to distinguish "has just been interned" from "was already in `tcx`",
260241
// so we track this in a separate set.
@@ -336,7 +317,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
336317
// okay with losing some potential for immutability here. This can anyway only affect
337318
// `static mut`.
338319
just_interned.insert(alloc_id);
339-
let next = intern_shallow(ecx, alloc_id, inner_mutability, disambiguator.as_deref_mut())?;
320+
let next = intern_shallow(ecx, alloc_id, inner_mutability)?;
340321
todo.extend(next);
341322
}
342323
if found_bad_mutable_ptr {
@@ -366,7 +347,7 @@ pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
366347
return interp_ok(());
367348
}
368349
// Move allocation to `tcx`.
369-
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not, None).unwrap().next() {
350+
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not).unwrap().next() {
370351
// We are not doing recursive interning, so we don't currently support provenance.
371352
// (If this assertion ever triggers, we should just implement a
372353
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.
@@ -391,7 +372,7 @@ impl<'tcx> InterpCx<'tcx, DummyMachine> {
391372
let dest = self.allocate(layout, MemoryKind::Stack)?;
392373
f(self, &dest.clone().into())?;
393374
let alloc_id = dest.ptr().provenance.unwrap().alloc_id(); // this was just allocated, it must have provenance
394-
for prov in intern_shallow(self, alloc_id, Mutability::Not, None).unwrap() {
375+
for prov in intern_shallow(self, alloc_id, Mutability::Not).unwrap() {
395376
// We are not doing recursive interning, so we don't currently support provenance.
396377
// (If this assertion ever triggers, we should just implement a
397378
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use rustc_ast::visit::walk_list;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1515
use rustc_errors::ErrorGuaranteed;
1616
use rustc_hir::def::{DefKind, Res};
17-
use rustc_hir::def_id::LocalDefIdMap;
18-
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorsMap};
17+
use rustc_hir::definitions::DefPathData;
1918
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
2019
use rustc_hir::{
2120
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
@@ -31,7 +30,6 @@ use rustc_span::{Ident, Span, sym};
3130
use tracing::{debug, debug_span, instrument};
3231

3332
use crate::errors;
34-
use crate::hir::definitions::PerParentDisambiguatorState;
3533

3634
#[extension(trait RegionExt)]
3735
impl ResolvedArg {
@@ -66,7 +64,6 @@ impl ResolvedArg {
6664
struct BoundVarContext<'a, 'tcx> {
6765
tcx: TyCtxt<'tcx>,
6866
rbv: &'a mut ResolveBoundVars<'tcx>,
69-
disambiguators: &'a mut LocalDefIdMap<PerParentDisambiguatorState>,
7067
scope: ScopeRef<'a, 'tcx>,
7168
opaque_capture_errors: RefCell<Option<OpaqueHigherRankedLifetimeCaptureErrors>>,
7269
}
@@ -261,7 +258,6 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
261258
tcx,
262259
rbv: &mut rbv,
263260
scope: &Scope::Root { opt_parent_item: None },
264-
disambiguators: &mut Default::default(),
265261
opaque_capture_errors: RefCell::new(None),
266262
};
267263
match tcx.hir_owner_node(local_def_id) {
@@ -1106,12 +1102,11 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11061102
where
11071103
F: for<'b> FnOnce(&mut BoundVarContext<'b, 'tcx>),
11081104
{
1109-
let BoundVarContext { tcx, rbv, disambiguators, .. } = self;
1105+
let BoundVarContext { tcx, rbv, .. } = self;
11101106
let nested_errors = RefCell::new(self.opaque_capture_errors.borrow_mut().take());
11111107
let mut this = BoundVarContext {
11121108
tcx: *tcx,
11131109
rbv,
1114-
disambiguators,
11151110
scope: &wrap_scope,
11161111
opaque_capture_errors: nested_errors,
11171112
};
@@ -1519,13 +1514,12 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15191514
// `opaque_def_id` is unique to the `BoundVarContext` pass which is executed once
15201515
// per `resolve_bound_vars` query. This is the only location that creates
15211516
// `OpaqueLifetime` paths. `<opaque_def_id>::OpaqueLifetime(..)` is thus unique
1522-
// to this query and duplicates within the query are handled by `self.disambiguator`.
1517+
// to this query.
15231518
let feed = self.tcx.create_def(
15241519
opaque_def_id,
15251520
None,
15261521
DefKind::LifetimeParam,
15271522
Some(DefPathData::OpaqueLifetime(ident.name)),
1528-
self.disambiguators.get_or_create(opaque_def_id),
15291523
);
15301524
feed.def_span(ident.span);
15311525
feed.def_ident_span(Some(ident.span));

compiler/rustc_middle/src/ty/context.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ use rustc_data_structures::sync::{
3232
};
3333
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, MultiSpan};
3434
use rustc_hir::def::DefKind;
35-
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, StableCrateIdMap};
36-
use rustc_hir::definitions::{DefPathData, Definitions, PerParentDisambiguatorState};
35+
use rustc_hir::def_id::{
36+
CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap, StableCrateIdMap,
37+
};
38+
use rustc_hir::definitions::{
39+
DefPathData, Definitions, PerParentDisambiguatorState, PerParentDisambiguatorsMap,
40+
};
3741
use rustc_hir::intravisit::VisitorExt;
3842
use rustc_hir::lang_items::LangItem;
3943
use rustc_hir::limit::Limit;
@@ -857,6 +861,9 @@ pub struct GlobalCtxt<'tcx> {
857861
pub(crate) hooks: crate::hooks::Providers,
858862

859863
untracked: Untracked,
864+
/// This is shared untracked state for creating new definitions.
865+
/// It should only be accessed by the `create_def_raw` query.
866+
untracked_disambiguator_state: Lock<LocalDefIdMap<PerParentDisambiguatorState>>,
860867

861868
pub query_system: QuerySystem<'tcx>,
862869
pub(crate) dep_kind_vtables: &'tcx [DepKindVTable<'tcx>],
@@ -1118,6 +1125,7 @@ impl<'tcx> TyCtxt<'tcx> {
11181125
lifetimes: common_lifetimes,
11191126
consts: common_consts,
11201127
untracked,
1128+
untracked_disambiguator_state: Lock::new(Default::default()),
11211129
query_system,
11221130
dep_kind_vtables,
11231131
ty_rcache: Default::default(),
@@ -1428,10 +1436,9 @@ impl<'tcx> TyCtxtAt<'tcx> {
14281436
name: Option<Symbol>,
14291437
def_kind: DefKind,
14301438
override_def_path_data: Option<DefPathData>,
1431-
disambiguator: &mut PerParentDisambiguatorState,
14321439
) -> TyCtxtFeed<'tcx, LocalDefId> {
14331440
let feed =
1434-
self.tcx.create_def(parent, name, def_kind, override_def_path_data, disambiguator);
1441+
self.tcx.create_def(parent, name, def_kind, override_def_path_data);
14351442

14361443
feed.def_span(self.span);
14371444
feed
@@ -1446,9 +1453,11 @@ impl<'tcx> TyCtxt<'tcx> {
14461453
name: Option<Symbol>,
14471454
def_kind: DefKind,
14481455
override_def_path_data: Option<DefPathData>,
1449-
disambiguator: &mut PerParentDisambiguatorState,
14501456
) -> TyCtxtFeed<'tcx, LocalDefId> {
14511457
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
1458+
let mut disambiguators = self.untracked_disambiguator_state.lock();
1459+
let disambiguator = disambiguators.get_or_create(parent);
1460+
14521461
// The following call has the side effect of modifying the tables inside `definitions`.
14531462
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
14541463
// decode the on-disk cache.

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_hir as hir;
4040
use rustc_hir::attrs::StrippedCfgItem;
4141
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
4242
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
43-
use rustc_hir::definitions::PerParentDisambiguatorState;
4443
use rustc_hir::{LangItem, attrs as attr, find_attr};
4544
use rustc_index::IndexVec;
4645
use rustc_index::bit_set::BitMatrix;
@@ -226,8 +225,6 @@ pub struct ResolverAstLowering<'tcx> {
226225

227226
// Information about delegations which is used when handling recursive delegations
228227
pub delegation_infos: LocalDefIdMap<DelegationInfo>,
229-
230-
pub disambiguators: LocalDefIdMap<Steal<PerParentDisambiguatorState>>,
231228
}
232229

233230
#[derive(Debug)]

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ use rustc_data_structures::steal::Steal;
7272
use rustc_data_structures::unord::UnordMap;
7373
use rustc_hir::def::DefKind;
7474
use rustc_hir::def_id::{DefId, LocalDefId};
75-
use rustc_hir::definitions::PerParentDisambiguatorState;
7675
use rustc_hir::{self as hir};
7776
use rustc_middle::bug;
7877
use rustc_middle::hir::place::{Projection, ProjectionKind};
@@ -216,13 +215,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
216215

217216
// This path is unique since we're in a query so we'll only be called once with `parent_def_id`
218217
// and this is the only location creating `SyntheticCoroutineBody`.
219-
let body_def = tcx.create_def(
220-
parent_def_id,
221-
None,
222-
DefKind::SyntheticCoroutineBody,
223-
None,
224-
&mut PerParentDisambiguatorState::new(parent_def_id),
225-
);
218+
let body_def = tcx.create_def(parent_def_id, None, DefKind::SyntheticCoroutineBody, None);
226219
by_move_body.source =
227220
mir::MirSource::from_instance(InstanceKind::Item(body_def.def_id().to_def_id()));
228221

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ use rustc_hir::def::{
5757
PerNS,
5858
};
5959
use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
60-
use rustc_hir::definitions::{PerParentDisambiguatorState, PerParentDisambiguatorsMap};
6160
use rustc_hir::{PrimTy, TraitCandidate, find_attr};
6261
use rustc_index::bit_set::DenseBitSet;
6362
use rustc_metadata::creader::CStore;
@@ -1424,8 +1423,6 @@ pub struct Resolver<'ra, 'tcx> {
14241423

14251424
node_id_to_def_id: NodeMap<LocalDefId>,
14261425

1427-
disambiguators: LocalDefIdMap<PerParentDisambiguatorState>,
1428-
14291426
/// Indices of unnamed struct or variant fields with unresolved attributes.
14301427
placeholder_field_indices: FxHashMap<NodeId, usize> = default::fx_hash_map(),
14311428
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
@@ -1619,10 +1616,8 @@ impl<'tcx> Resolver<'_, 'tcx> {
16191616
self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id]),
16201617
);
16211618

1622-
let disambiguator = self.disambiguators.get_or_create(parent);
1623-
16241619
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
1625-
let feed = self.tcx.create_def(parent, name, def_kind, None, disambiguator);
1620+
let feed = self.tcx.create_def(parent, name, def_kind, None);
16261621
let def_id = feed.def_id();
16271622

16281623
// Create the definition.
@@ -1805,7 +1800,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18051800
doc_link_resolutions: Default::default(),
18061801
doc_link_traits_in_scope: Default::default(),
18071802
current_crate_outer_attr_insert_span,
1808-
disambiguators: Default::default(),
18091803
..
18101804
};
18111805

@@ -1908,11 +1902,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19081902
Some(StrippedCfgItem { parent_scope, ident: item.ident, cfg: item.cfg })
19091903
})
19101904
.collect();
1911-
let disambiguators = self
1912-
.disambiguators
1913-
.into_items()
1914-
.map(|(def_id, disamb)| (def_id, Steal::new(disamb)))
1915-
.collect();
19161905

19171906
let global_ctxt = ResolverGlobalCtxt {
19181907
expn_that_defined,
@@ -1944,7 +1933,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19441933
lifetime_elision_allowed: self.lifetime_elision_allowed,
19451934
lint_buffer: Steal::new(self.lint_buffer),
19461935
delegation_infos: self.delegation_infos,
1947-
disambiguators,
19481936
};
19491937
ResolverOutputs { global_ctxt, ast_lowering }
19501938
}

0 commit comments

Comments
 (0)