Skip to content

Commit 2690113

Browse files
committed
Better disambiguators
1 parent e22c616 commit 2690113

8 files changed

Lines changed: 64 additions & 35 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ 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;
54+
use rustc_hir::definitions::{Disambiguator, PerParentDisambiguatorState};
5555
use rustc_hir::lints::{AttributeLint, DelayedLint, DynAttribute};
5656
use rustc_hir::{
5757
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
@@ -410,7 +410,12 @@ impl<'tcx> ResolverAstLowering<'tcx> {
410410
}
411411

412412
fn steal_or_create_disambiguator(&self, parent: LocalDefId) -> PerParentDisambiguatorState {
413-
self.per_parent_disambiguators.get(&parent).map(|s| s.steal()).unwrap_or_default()
413+
self.per_parent_disambiguators.get(&parent).map(|s| s.steal()).unwrap_or_else(|| {
414+
PerParentDisambiguatorState::new(
415+
#[cfg(debug_assertions)]
416+
parent,
417+
)
418+
})
414419
}
415420
}
416421

@@ -740,7 +745,13 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
740745
let def_id = self
741746
.tcx
742747
.at(span)
743-
.create_def(parent, name, def_kind, None, &mut self.disambiguator)
748+
.create_def(
749+
parent,
750+
name,
751+
def_kind,
752+
None,
753+
Disambiguator::PerParent(&mut self.disambiguator),
754+
)
744755
.def_id();
745756

746757
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir::def::DefKind;
1717
use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1919
use rustc_hir as hir;
20-
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
20+
use rustc_hir::definitions::{DefPathData, Disambiguator, DisambiguatorState};
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2222
use rustc_middle::mir::interpret::{
2323
AllocBytes, ConstAllocation, CtfeProvenance, InterpResult, Provenance,
@@ -155,7 +155,7 @@ fn intern_as_new_static<'tcx>(
155155
None,
156156
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
157157
Some(DefPathData::NestedStatic),
158-
disambiguator,
158+
Disambiguator::Full(disambiguator),
159159
);
160160
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
161161

compiler/rustc_hir/src/definitions.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use std::fmt::{self, Write};
88
use std::hash::Hash;
99

10+
use rustc_data_structures::fx::FxHashMap;
1011
use rustc_data_structures::stable_hasher::StableHasher;
11-
use rustc_data_structures::unord::UnordMap;
1212
use rustc_hashes::Hash64;
1313
use rustc_index::IndexVec;
1414
use rustc_macros::{BlobDecodable, Decodable, Encodable};
@@ -97,36 +97,35 @@ impl DefPathTable {
9797
}
9898
}
9999

100-
pub trait Disambiguator {
101-
fn entry(&mut self, parent: LocalDefId, data: DefPathData) -> &mut u32;
100+
pub enum Disambiguator<'a> {
101+
PerParent(&'a mut PerParentDisambiguatorState),
102+
Full(&'a mut DisambiguatorState),
102103
}
103104

104105
#[derive(Debug, Default, Clone)]
105106
pub struct PerParentDisambiguatorState {
106-
next: UnordMap<DefPathData, u32>,
107+
#[cfg(debug_assertions)]
108+
parent: Option<LocalDefId>,
109+
next: FxHashMap<DefPathData, u32>,
107110
}
108111

109-
impl Disambiguator for PerParentDisambiguatorState {
110-
#[inline]
111-
fn entry(&mut self, _: LocalDefId, data: DefPathData) -> &mut u32 {
112-
self.next.entry(data).or_insert(0)
112+
impl PerParentDisambiguatorState {
113+
pub fn new(#[cfg(debug_assertions)] parent: LocalDefId) -> PerParentDisambiguatorState {
114+
PerParentDisambiguatorState {
115+
#[cfg(debug_assertions)]
116+
parent: Some(parent),
117+
next: Default::default(),
118+
}
113119
}
114120
}
115121

116122
#[derive(Debug, Default, Clone)]
117123
pub struct DisambiguatorState {
118-
next: UnordMap<(LocalDefId, DefPathData), u32>,
119-
}
120-
121-
impl Disambiguator for DisambiguatorState {
122-
#[inline]
123-
fn entry(&mut self, parent: LocalDefId, data: DefPathData) -> &mut u32 {
124-
self.next.entry((parent, data)).or_insert(0)
125-
}
124+
next: FxHashMap<(LocalDefId, DefPathData), u32>,
126125
}
127126

128127
impl DisambiguatorState {
129-
pub const fn new() -> Self {
128+
pub fn new() -> Self {
130129
Self { next: Default::default() }
131130
}
132131

@@ -408,7 +407,7 @@ impl Definitions {
408407
&mut self,
409408
parent: LocalDefId,
410409
data: DefPathData,
411-
disambiguator: &mut impl Disambiguator,
410+
disambiguator: Disambiguator<'_>,
412411
) -> LocalDefId {
413412
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
414413
// reference to `Definitions` and we're already holding a mutable reference.
@@ -422,7 +421,19 @@ impl Definitions {
422421

423422
// Find the next free disambiguator for this key.
424423
let disambiguator = {
425-
let next_disamb = disambiguator.entry(parent, data);
424+
let next_disamb = match disambiguator {
425+
Disambiguator::PerParent(d) => {
426+
debug_assert_eq!(
427+
parent,
428+
d.parent.expect("must be set"),
429+
"provided parent and parent in disambiguator must be the same"
430+
);
431+
432+
d.next.entry(data).or_insert(0)
433+
}
434+
Disambiguator::Full(d) => d.next.entry((parent, data)).or_insert(0),
435+
};
436+
426437
let disambiguator = *next_disamb;
427438
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
428439
disambiguator

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +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::definitions::{DefPathData, DisambiguatorState};
17+
use rustc_hir::definitions::{DefPathData, Disambiguator, DisambiguatorState};
1818
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
1919
use rustc_hir::{
2020
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
@@ -1523,7 +1523,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15231523
None,
15241524
DefKind::LifetimeParam,
15251525
Some(DefPathData::OpaqueLifetime(ident.name)),
1526-
self.disambiguator,
1526+
Disambiguator::Full(self.disambiguator),
15271527
);
15281528
feed.def_span(ident.span);
15291529
feed.def_ident_span(Some(ident.span));

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
13981398
name: Option<Symbol>,
13991399
def_kind: DefKind,
14001400
override_def_path_data: Option<DefPathData>,
1401-
disambiguator: &mut impl Disambiguator,
1401+
disambiguator: Disambiguator<'_>,
14021402
) -> TyCtxtFeed<'tcx, LocalDefId> {
14031403
let feed =
14041404
self.tcx.create_def(parent, name, def_kind, override_def_path_data, disambiguator);
@@ -1416,7 +1416,7 @@ impl<'tcx> TyCtxt<'tcx> {
14161416
name: Option<Symbol>,
14171417
def_kind: DefKind,
14181418
override_def_path_data: Option<DefPathData>,
1419-
disambiguator: &mut impl Disambiguator,
1419+
disambiguator: Disambiguator<'_>,
14201420
) -> TyCtxtFeed<'tcx, LocalDefId> {
14211421
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
14221422
// The following call has the side effect of modifying the tables inside `definitions`.

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use rustc_data_structures::unord::UnordMap;
7373
use rustc_hir as hir;
7474
use rustc_hir::def::DefKind;
7575
use rustc_hir::def_id::{DefId, LocalDefId};
76-
use rustc_hir::definitions::DisambiguatorState;
76+
use rustc_hir::definitions::{Disambiguator, DisambiguatorState};
7777
use rustc_middle::bug;
7878
use rustc_middle::hir::place::{Projection, ProjectionKind};
7979
use rustc_middle::mir::visit::MutVisitor;
@@ -221,7 +221,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
221221
None,
222222
DefKind::SyntheticCoroutineBody,
223223
None,
224-
&mut DisambiguatorState::new(),
224+
Disambiguator::Full(&mut DisambiguatorState::new()),
225225
);
226226
by_move_body.source =
227227
mir::MirSource::from_instance(InstanceKind::Item(body_def.def_id().to_def_id()));

compiler/rustc_resolve/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ 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;
60+
use rustc_hir::definitions::{Disambiguator, PerParentDisambiguatorState};
6161
use rustc_hir::{PrimTy, TraitCandidate, find_attr};
6262
use rustc_index::bit_set::DenseBitSet;
6363
use rustc_metadata::creader::CStore;
@@ -1620,13 +1620,20 @@ impl<'tcx> Resolver<'_, 'tcx> {
16201620
self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id].key()),
16211621
);
16221622

1623+
let disambiguator = self.per_parent_disambiguators.entry(parent).or_insert_with(|| {
1624+
PerParentDisambiguatorState::new(
1625+
#[cfg(debug_assertions)]
1626+
parent,
1627+
)
1628+
});
1629+
16231630
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
16241631
let feed = self.tcx.create_def(
16251632
parent,
16261633
name,
16271634
def_kind,
16281635
None,
1629-
self.per_parent_disambiguators.entry(parent).or_default(),
1636+
Disambiguator::PerParent(disambiguator),
16301637
);
16311638
let def_id = feed.def_id();
16321639

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
3-
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
3+
use rustc_hir::definitions::{DefPathData, Disambiguator, DisambiguatorState};
44
use rustc_hir::intravisit::{self, Visitor};
55
use rustc_hir::{self as hir, ConstItemRhs, ImplItemImplKind, ItemKind};
66
use rustc_middle::query::Providers;
@@ -240,7 +240,7 @@ fn associated_type_for_impl_trait_in_trait(
240240
None,
241241
DefKind::AssocTy,
242242
Some(data),
243-
disambiguator,
243+
Disambiguator::Full(disambiguator),
244244
);
245245

246246
let local_def_id = trait_assoc_ty.def_id();
@@ -306,7 +306,7 @@ fn associated_type_for_impl_trait_in_impl(
306306
None,
307307
DefKind::AssocTy,
308308
Some(data),
309-
disambiguator,
309+
Disambiguator::Full(disambiguator),
310310
);
311311

312312
let local_def_id = impl_assoc_ty.def_id();

0 commit comments

Comments
 (0)