Skip to content

Commit 070d246

Browse files
committed
Use default disambiguator for everything instead of delegations
1 parent eaaf4bb commit 070d246

11 files changed

Lines changed: 114 additions & 78 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
88
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
99
use rustc_hir::def::{DefKind, PerNS, Res};
1010
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
11+
use rustc_hir::definitions::Disambiguator;
1112
use rustc_hir::{
1213
self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target, find_attr,
1314
};
@@ -78,9 +79,10 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
7879
fn with_lctx(
7980
&mut self,
8081
owner: NodeId,
82+
disambiguator: &mut Disambiguator,
8183
f: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::OwnerNode<'hir>,
8284
) {
83-
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
85+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, disambiguator);
8486
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
8587

8688
for (def_id, info) in lctx.children {
@@ -93,28 +95,28 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
9395
}
9496
}
9597

96-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
98+
pub(super) fn lower_node(&mut self, def_id: LocalDefId, disambiguator: &mut Disambiguator) {
9799
let owner = self.owners.get_or_insert_mut(def_id);
98100
if let hir::MaybeOwner::Phantom = owner {
99101
let node = self.ast_index[def_id];
100102
match node {
101103
AstOwner::NonOwner => {}
102104
AstOwner::Crate(c) => {
103105
assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
104-
self.with_lctx(CRATE_NODE_ID, |lctx| {
106+
self.with_lctx(CRATE_NODE_ID, disambiguator, |lctx| {
105107
let module = lctx.lower_mod(&c.items, &c.spans);
106108
// FIXME(jdonszelman): is dummy span ever a problem here?
107109
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, Target::Crate);
108110
hir::OwnerNode::Crate(module)
109111
})
110112
}
111-
AstOwner::Item(item) => {
112-
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
113-
}
113+
AstOwner::Item(item) => self.with_lctx(item.id, disambiguator, |lctx| {
114+
hir::OwnerNode::Item(lctx.lower_item(item))
115+
}),
114116
AstOwner::AssocItem(item, ctxt) => {
115-
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
117+
self.with_lctx(item.id, disambiguator, |lctx| lctx.lower_assoc_item(item, ctxt))
116118
}
117-
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
119+
AstOwner::ForeignItem(item) => self.with_lctx(item.id, disambiguator, |lctx| {
118120
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
119121
}),
120122
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub mod stability;
9494
struct LoweringContext<'a, 'hir, R> {
9595
tcx: TyCtxt<'hir>,
9696
resolver: &'a mut R,
97-
disambiguator: PerParentDisambiguatorState,
97+
disambiguator: &'a mut Disambiguator,
9898

9999
/// Used to allocate HIR nodes.
100100
arena: &'hir hir::Arena<'hir>,
@@ -154,12 +154,12 @@ struct LoweringContext<'a, 'hir, R> {
154154
}
155155

156156
impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
157-
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut R) -> Self {
157+
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut R, disambiguator: &'a mut Disambiguator) -> Self {
158158
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
159159
Self {
160160
tcx,
161161
resolver,
162-
disambiguator: Default::default(),
162+
disambiguator,
163163
arena: tcx.hir_arena,
164164

165165
// HirId handling.
@@ -302,10 +302,6 @@ impl<'a, 'tcx> ResolverAstLoweringExt<'tcx> for ResolverDelayedAstLowering<'a, '
302302
fn next_node_id(&mut self) -> NodeId {
303303
next_node_id(&mut self.next_node_id)
304304
}
305-
306-
fn steal_or_create_disambiguator(&self, parent: LocalDefId) -> PerParentDisambiguatorState {
307-
self.base.steal_or_create_disambiguator(parent)
308-
}
309305
}
310306

311307
fn next_node_id(current_id: &mut NodeId) -> NodeId {
@@ -408,15 +404,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
408404
fn next_node_id(&mut self) -> NodeId {
409405
next_node_id(&mut self.next_node_id)
410406
}
411-
412-
fn steal_or_create_disambiguator(&self, parent: LocalDefId) -> PerParentDisambiguatorState {
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-
})
419-
}
420407
}
421408

422409
/// How relaxed bounds `?Trait` should be treated.
@@ -637,6 +624,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
637624
tcx.definitions_untracked().def_index_count(),
638625
);
639626

627+
let mut disambiguator = Disambiguator::Full(resolver.disambiguator.steal());
640628
let mut lowerer = item::ItemLowerer {
641629
tcx,
642630
resolver: &mut resolver,
@@ -652,7 +640,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
652640
| AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation { .. }, .. }, _) => {
653641
delayed_ids.insert(def_id);
654642
}
655-
_ => lowerer.lower_node(def_id),
643+
_ => lowerer.lower_node(def_id, &mut disambiguator),
656644
};
657645
}
658646

@@ -683,14 +671,23 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
683671

684672
let mut map = Default::default();
685673

674+
let per_parent_disambiguators = &resolver.base.per_parent_disambiguators;
675+
let disambiguator =
676+
per_parent_disambiguators.get(&def_id).map(Steal::steal).unwrap_or_else(|| {
677+
PerParentDisambiguatorState::new(
678+
#[cfg(debug_assertions)]
679+
def_id,
680+
)
681+
});
682+
686683
let mut lowerer = item::ItemLowerer {
687684
tcx,
688685
resolver: &mut resolver,
689686
ast_index: &ast_index,
690687
owners: Owners::Map(&mut map),
691688
};
692689

693-
lowerer.lower_node(def_id);
690+
lowerer.lower_node(def_id, &mut Disambiguator::PerParent(disambiguator));
694691

695692
for (child_def_id, owner) in map {
696693
tcx.feed_delayed_owner(child_def_id, owner);
@@ -745,13 +742,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
745742
let def_id = self
746743
.tcx
747744
.at(span)
748-
.create_def(
749-
parent,
750-
name,
751-
def_kind,
752-
None,
753-
Disambiguator::PerParent(&mut self.disambiguator),
754-
)
745+
.create_def(parent, name, def_kind, None, &mut self.disambiguator)
755746
.def_id();
756747

757748
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
@@ -792,8 +783,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
792783
) {
793784
let owner_id = self.owner_id(owner);
794785

795-
let new_disambig = self.resolver.steal_or_create_disambiguator(owner_id.def_id);
796-
let disambiguator = std::mem::replace(&mut self.disambiguator, new_disambig);
797786
let current_attrs = std::mem::take(&mut self.attrs);
798787
let current_bodies = std::mem::take(&mut self.bodies);
799788
let current_define_opaque = std::mem::take(&mut self.define_opaque);
@@ -828,7 +817,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
828817
assert!(self.impl_trait_bounds.is_empty());
829818
let info = self.make_owner_info(item);
830819

831-
self.disambiguator = disambiguator;
832820
self.attrs = current_attrs;
833821
self.bodies = current_bodies;
834822
self.define_opaque = current_define_opaque;

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
105105
ecx: &mut InterpCx<'tcx, M>,
106106
alloc_id: AllocId,
107107
mutability: Mutability,
108-
disambiguator: Option<&mut DisambiguatorState>,
108+
disambiguator: Option<&mut Disambiguator>,
109109
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
110110
trace!("intern_shallow {:?}", alloc_id);
111111
// remove allocation
@@ -144,7 +144,7 @@ fn intern_as_new_static<'tcx>(
144144
static_id: LocalDefId,
145145
alloc_id: AllocId,
146146
alloc: ConstAllocation<'tcx>,
147-
disambiguator: &mut DisambiguatorState,
147+
disambiguator: &mut Disambiguator,
148148
) {
149149
// `intern_const_alloc_recursive` is called once per static and it contains the `DisambiguatorState`.
150150
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
@@ -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::Full(disambiguator),
158+
disambiguator,
159159
);
160160
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
161161

@@ -205,7 +205,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
205205
intern_kind: InternKind,
206206
ret: &MPlaceTy<'tcx>,
207207
) -> Result<(), InternError> {
208-
let mut disambiguator = DisambiguatorState::new();
208+
let mut disambiguator = Disambiguator::Full(DisambiguatorState::new());
209209

210210
// We are interning recursively, and for mutability we are distinguishing the "root" allocation
211211
// that we are starting in, and all other allocations that we are encountering recursively.

compiler/rustc_hir/src/definitions.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,25 @@ impl DefPathTable {
9797
}
9898
}
9999

100-
pub enum Disambiguator<'a> {
101-
PerParent(&'a mut PerParentDisambiguatorState),
102-
Full(&'a mut DisambiguatorState),
100+
pub enum Disambiguator {
101+
PerParent(PerParentDisambiguatorState),
102+
Full(DisambiguatorState),
103+
}
104+
105+
impl Disambiguator {
106+
pub fn expect_full(self) -> DisambiguatorState {
107+
match self {
108+
Disambiguator::PerParent(_) => panic!("expected full disambiguator"),
109+
Disambiguator::Full(d) => d,
110+
}
111+
}
112+
113+
pub fn expect_per_parent(self) -> PerParentDisambiguatorState {
114+
match self {
115+
Disambiguator::PerParent(d) => d,
116+
Disambiguator::Full(_) => panic!("expected per-parent disambiguator"),
117+
}
118+
}
103119
}
104120

105121
#[derive(Debug, Default, Clone)]
@@ -407,7 +423,7 @@ impl Definitions {
407423
&mut self,
408424
parent: LocalDefId,
409425
data: DefPathData,
410-
disambiguator: Disambiguator<'_>,
426+
disambiguator: &mut Disambiguator,
411427
) -> LocalDefId {
412428
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
413429
// reference to `Definitions` and we're already holding a mutable reference.

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl ResolvedArg {
6464
struct BoundVarContext<'a, 'tcx> {
6565
tcx: TyCtxt<'tcx>,
6666
rbv: &'a mut ResolveBoundVars<'tcx>,
67-
disambiguator: &'a mut DisambiguatorState,
67+
disambiguator: &'a mut Disambiguator,
6868
scope: ScopeRef<'a, 'tcx>,
6969
opaque_capture_errors: RefCell<Option<OpaqueHigherRankedLifetimeCaptureErrors>>,
7070
}
@@ -259,7 +259,7 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
259259
tcx,
260260
rbv: &mut rbv,
261261
scope: &Scope::Root { opt_parent_item: None },
262-
disambiguator: &mut DisambiguatorState::new(),
262+
disambiguator: &mut Disambiguator::Full(DisambiguatorState::new()),
263263
opaque_capture_errors: RefCell::new(None),
264264
};
265265
match tcx.hir_owner_node(local_def_id) {
@@ -1523,7 +1523,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15231523
None,
15241524
DefKind::LifetimeParam,
15251525
Some(DefPathData::OpaqueLifetime(ident.name)),
1526-
Disambiguator::Full(self.disambiguator),
1526+
&mut 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: Disambiguator<'_>,
1401+
disambiguator: &mut 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: Disambiguator<'_>,
1419+
disambiguator: &mut 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_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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;
43+
use rustc_hir::definitions::{DisambiguatorState, PerParentDisambiguatorState};
4444
use rustc_hir::{LangItem, attrs as attr, find_attr};
4545
use rustc_index::IndexVec;
4646
use rustc_index::bit_set::BitMatrix;
@@ -227,6 +227,7 @@ pub struct ResolverAstLowering<'tcx> {
227227
// Information about delegations which is used when handling recursive delegations
228228
pub delegation_infos: LocalDefIdMap<DelegationInfo>,
229229

230+
pub disambiguator: Steal<DisambiguatorState>,
230231
pub per_parent_disambiguators: LocalDefIdMap<Steal<PerParentDisambiguatorState>>,
231232
}
232233

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
221221
None,
222222
DefKind::SyntheticCoroutineBody,
223223
None,
224-
Disambiguator::Full(&mut DisambiguatorState::new()),
224+
&mut Disambiguator::Full(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/def_collector.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ struct DefCollector<'a, 'ra, 'tcx> {
3333
expansion: LocalExpnId,
3434
}
3535

36+
struct ParentUpdate {
37+
def_id: LocalDefId,
38+
is_delegation: bool,
39+
}
40+
41+
impl Into<ParentUpdate> for LocalDefId {
42+
fn into(self) -> ParentUpdate {
43+
ParentUpdate { def_id: self, is_delegation: false }
44+
}
45+
}
46+
3647
impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
3748
fn create_def(
3849
&mut self,
@@ -54,13 +65,21 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
5465
def_kind,
5566
self.expansion.to_expn_id(),
5667
span.with_parent(None),
68+
self.invocation_parent.is_delegation,
5769
)
5870
.def_id()
5971
}
6072

61-
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
62-
let orig_parent_def = mem::replace(&mut self.invocation_parent.parent_def, parent_def);
73+
fn with_parent<F: FnOnce(&mut Self)>(&mut self, update: impl Into<ParentUpdate>, f: F) {
74+
let update = update.into();
75+
76+
let orig_parent_def = mem::replace(&mut self.invocation_parent.parent_def, update.def_id);
77+
let orig_is_delegation =
78+
mem::replace(&mut self.invocation_parent.is_delegation, update.is_delegation);
79+
6380
f(self);
81+
82+
self.invocation_parent.is_delegation = orig_is_delegation;
6483
self.invocation_parent.parent_def = orig_parent_def;
6584
}
6685

@@ -184,7 +203,10 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
184203
self.resolver.new_local_macro(def_id, macro_data);
185204
}
186205

187-
self.with_parent(def_id, |this| {
206+
let is_delegation = matches!(i.kind, ItemKind::Delegation(..));
207+
let update = ParentUpdate { is_delegation, def_id };
208+
209+
self.with_parent(update, |this| {
188210
this.with_impl_trait(ImplTraitContext::Existential, |this| {
189211
match i.kind {
190212
ItemKind::Struct(_, _, ref struct_def)
@@ -370,8 +392,11 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
370392
}
371393
};
372394

373-
let def = self.create_def(i.id, Some(ident.name), def_kind, i.span);
374-
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
395+
let is_delegation = matches!(i.kind, AssocItemKind::Delegation(..));
396+
let def_id = self.create_def(i.id, Some(ident.name), def_kind, i.span);
397+
let update = ParentUpdate { is_delegation, def_id };
398+
399+
self.with_parent(update, |this| visit::walk_assoc_item(this, i, ctxt));
375400
}
376401

377402
fn visit_pat(&mut self, pat: &'a Pat) {

0 commit comments

Comments
 (0)