Skip to content

Commit 5d9cfeb

Browse files
committed
Fix delegation def path hash collision, add per-parent disambiguators
1 parent 7659cec commit 5d9cfeb

14 files changed

Lines changed: 135 additions & 53 deletions

File tree

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,9 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
295295

296296
let param_ident = Ident::new(p.name, span);
297297
let def_name = Some(param_ident.name);
298-
let path_data = def_kind.def_path_data(def_name);
299298
let node_id = self.next_node_id();
300299

301-
let def_id = self.create_def(node_id, def_name, def_kind, path_data, span);
300+
let def_id = self.create_def(node_id, def_name, def_kind, span);
302301

303302
let kind = match p.kind {
304303
GenericParamDefKind::Lifetime => {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
88
use rustc_errors::msg;
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, Res};
11-
use rustc_hir::definitions::DefPathData;
1211
use rustc_hir::{HirId, Target, find_attr};
1312
use rustc_middle::span_bug;
1413
use rustc_middle::ty::TyCtxt;
@@ -472,13 +471,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
472471
for (idx, arg) in args.iter().cloned().enumerate() {
473472
if legacy_args_idx.contains(&idx) {
474473
let node_id = self.next_node_id();
475-
self.create_def(
476-
node_id,
477-
None,
478-
DefKind::AnonConst,
479-
DefPathData::LateAnonConst,
480-
f.span,
481-
);
474+
self.create_def(node_id, None, DefKind::AnonConst, f.span);
482475
let mut visitor = WillCreateDefIdsVisitor {};
483476
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
484477
Box::new(Expr {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 16 additions & 13 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::{DefPathData, DisambiguatorState};
54+
use rustc_hir::definitions::PerParentDisambiguatorState;
5555
use rustc_hir::lints::{AttributeLint, DelayedLint};
5656
use rustc_hir::{
5757
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
@@ -94,7 +94,7 @@ pub mod stability;
9494
struct LoweringContext<'a, 'hir, R> {
9595
tcx: TyCtxt<'hir>,
9696
resolver: &'a mut R,
97-
disambiguator: DisambiguatorState,
97+
disambiguator: PerParentDisambiguatorState,
9898

9999
/// Used to allocate HIR nodes.
100100
arena: &'hir hir::Arena<'hir>,
@@ -159,7 +159,7 @@ impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
159159
Self {
160160
tcx,
161161
resolver,
162-
disambiguator: DisambiguatorState::new(),
162+
disambiguator: Default::default(),
163163
arena: tcx.hir_arena,
164164

165165
// HirId handling.
@@ -302,6 +302,10 @@ 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+
}
305309
}
306310

307311
fn next_node_id(current_id: &mut NodeId) -> NodeId {
@@ -404,6 +408,10 @@ impl<'tcx> ResolverAstLowering<'tcx> {
404408
fn next_node_id(&mut self) -> NodeId {
405409
next_node_id(&mut self.next_node_id)
406410
}
411+
412+
fn steal_or_create_disambiguator(&self, parent: LocalDefId) -> PerParentDisambiguatorState {
413+
self.per_parent_disambiguators.get(&parent).map(|s| s.steal()).unwrap_or_default()
414+
}
407415
}
408416

409417
/// How relaxed bounds `?Trait` should be treated.
@@ -733,7 +741,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
733741
node_id: ast::NodeId,
734742
name: Option<Symbol>,
735743
def_kind: DefKind,
736-
def_path_data: DefPathData,
737744
span: Span,
738745
) -> LocalDefId {
739746
let parent = self.current_hir_id_owner.def_id;
@@ -749,7 +756,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
749756
let def_id = self
750757
.tcx
751758
.at(span)
752-
.create_def(parent, name, def_kind, Some(def_path_data), &mut self.disambiguator)
759+
.create_def(parent, name, def_kind, None, &mut self.disambiguator)
753760
.def_id();
754761

755762
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
@@ -790,6 +797,8 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
790797
) {
791798
let owner_id = self.owner_id(owner);
792799

800+
let new_disambig = self.resolver.steal_or_create_disambiguator(owner_id.def_id);
801+
let disambiguator = std::mem::replace(&mut self.disambiguator, new_disambig);
793802
let current_attrs = std::mem::take(&mut self.attrs);
794803
let current_bodies = std::mem::take(&mut self.bodies);
795804
let current_define_opaque = std::mem::take(&mut self.define_opaque);
@@ -824,6 +833,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
824833
assert!(self.impl_trait_bounds.is_empty());
825834
let info = self.make_owner_info(item);
826835

836+
self.disambiguator = disambiguator;
827837
self.attrs = current_attrs;
828838
self.bodies = current_bodies;
829839
self.define_opaque = current_define_opaque;
@@ -1031,7 +1041,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
10311041
param,
10321042
Some(kw::UnderscoreLifetime),
10331043
DefKind::LifetimeParam,
1034-
DefPathData::DesugaredAnonymousLifetime,
10351044
ident.span,
10361045
);
10371046
debug!(?_def_id);
@@ -2512,13 +2521,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
25122521
// We're lowering a const argument that was originally thought to be a type argument,
25132522
// so the def collector didn't create the def ahead of time. That's why we have to do
25142523
// it here.
2515-
let def_id = self.create_def(
2516-
node_id,
2517-
None,
2518-
DefKind::AnonConst,
2519-
DefPathData::LateAnonConst,
2520-
span,
2521-
);
2524+
let def_id = self.create_def(node_id, None, DefKind::AnonConst, span);
25222525
let hir_id = self.lower_node_id(node_id);
25232526

25242527
let path_expr = Expr {

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::sync::Arc;
33
use rustc_ast::*;
44
use rustc_data_structures::stack::ensure_sufficient_stack;
55
use rustc_hir::def::{DefKind, Res};
6-
use rustc_hir::definitions::DefPathData;
76
use rustc_hir::{self as hir, LangItem, Target};
87
use rustc_middle::span_bug;
98
use rustc_span::{DesugaringKind, Ident, Span, Spanned, respan};
@@ -534,8 +533,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
534533
// We're generating a range end that didn't exist in the AST,
535534
// so the def collector didn't create the def ahead of time. That's why we have to do
536535
// it here.
537-
let def_id =
538-
self.create_def(node_id, None, DefKind::AnonConst, DefPathData::LateAnonConst, span);
536+
let def_id = self.create_def(node_id, None, DefKind::AnonConst, span);
539537
let hir_id = self.lower_node_id(node_id);
540538

541539
let unstable_span = self.mark_span_with_reason(

compiler/rustc_hir/src/definitions.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,34 @@ impl DefPathTable {
9797
}
9898
}
9999

100-
#[derive(Debug)]
100+
pub trait Disambiguator {
101+
fn entry(&mut self, parent: LocalDefId, data: DefPathData) -> &mut u32;
102+
}
103+
104+
#[derive(Debug, Default, Clone)]
105+
pub struct PerParentDisambiguatorState {
106+
next: UnordMap<DefPathData, u32>,
107+
}
108+
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)
113+
}
114+
}
115+
116+
#[derive(Debug, Default, Clone)]
101117
pub struct DisambiguatorState {
102118
next: UnordMap<(LocalDefId, DefPathData), u32>,
103119
}
104120

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+
}
126+
}
127+
105128
impl DisambiguatorState {
106129
pub const fn new() -> Self {
107130
Self { next: Default::default() }
@@ -302,10 +325,6 @@ pub enum DefPathData {
302325
Ctor,
303326
/// A constant expression (see `{ast,hir}::AnonConst`).
304327
AnonConst,
305-
/// A constant expression created during AST->HIR lowering..
306-
LateAnonConst,
307-
/// A fresh anonymous lifetime created by desugaring elided lifetimes.
308-
DesugaredAnonymousLifetime,
309328
/// An existential `impl Trait` type node.
310329
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
311330
OpaqueTy,
@@ -389,7 +408,7 @@ impl Definitions {
389408
&mut self,
390409
parent: LocalDefId,
391410
data: DefPathData,
392-
disambiguator: &mut DisambiguatorState,
411+
disambiguator: &mut impl Disambiguator,
393412
) -> LocalDefId {
394413
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
395414
// reference to `Definitions` and we're already holding a mutable reference.
@@ -403,7 +422,7 @@ impl Definitions {
403422

404423
// Find the next free disambiguator for this key.
405424
let disambiguator = {
406-
let next_disamb = disambiguator.next.entry((parent, data)).or_insert(0);
425+
let next_disamb = disambiguator.entry(parent, data);
407426
let disambiguator = *next_disamb;
408427
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
409428
disambiguator
@@ -458,8 +477,6 @@ impl DefPathData {
458477
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
459478
| OpaqueLifetime(name) => Some(name),
460479

461-
DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
462-
463480
Impl
464481
| ForeignMod
465482
| CrateRoot
@@ -468,7 +485,6 @@ impl DefPathData {
468485
| Closure
469486
| Ctor
470487
| AnonConst
471-
| LateAnonConst
472488
| OpaqueTy
473489
| AnonAssocTy(..)
474490
| SyntheticCoroutineBody
@@ -482,8 +498,6 @@ impl DefPathData {
482498
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name)
483499
| OpaqueLifetime(name) => Some(name),
484500

485-
DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
486-
487501
Impl
488502
| ForeignMod
489503
| CrateRoot
@@ -492,7 +506,6 @@ impl DefPathData {
492506
| Closure
493507
| Ctor
494508
| AnonConst
495-
| LateAnonConst
496509
| OpaqueTy
497510
| SyntheticCoroutineBody
498511
| NestedStatic => None,
@@ -512,8 +525,7 @@ impl DefPathData {
512525
GlobalAsm => DefPathDataName::Anon { namespace: sym::global_asm },
513526
Closure => DefPathDataName::Anon { namespace: sym::closure },
514527
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
515-
AnonConst | LateAnonConst => DefPathDataName::Anon { namespace: sym::constant },
516-
DesugaredAnonymousLifetime => DefPathDataName::Named(kw::UnderscoreLifetime),
528+
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
517529
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
518530
AnonAssocTy(..) => DefPathDataName::Anon { namespace: sym::anon_assoc },
519531
SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic },

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15231523
None,
15241524
DefKind::LifetimeParam,
15251525
Some(DefPathData::OpaqueLifetime(ident.name)),
1526-
&mut self.disambiguator,
1526+
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_data_structures::sync::{
3232
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, MultiSpan};
3333
use rustc_hir::def::DefKind;
3434
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
35-
use rustc_hir::definitions::{DefPathData, Definitions, DisambiguatorState};
35+
use rustc_hir::definitions::{DefPathData, Definitions, Disambiguator};
3636
use rustc_hir::intravisit::VisitorExt;
3737
use rustc_hir::lang_items::LangItem;
3838
use rustc_hir::limit::Limit;
@@ -1355,7 +1355,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
13551355
name: Option<Symbol>,
13561356
def_kind: DefKind,
13571357
override_def_path_data: Option<DefPathData>,
1358-
disambiguator: &mut DisambiguatorState,
1358+
disambiguator: &mut impl Disambiguator,
13591359
) -> TyCtxtFeed<'tcx, LocalDefId> {
13601360
let feed =
13611361
self.tcx.create_def(parent, name, def_kind, override_def_path_data, disambiguator);
@@ -1373,7 +1373,7 @@ impl<'tcx> TyCtxt<'tcx> {
13731373
name: Option<Symbol>,
13741374
def_kind: DefKind,
13751375
override_def_path_data: Option<DefPathData>,
1376-
disambiguator: &mut DisambiguatorState,
1376+
disambiguator: &mut impl Disambiguator,
13771377
) -> TyCtxtFeed<'tcx, LocalDefId> {
13781378
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
13791379
// The following call has the side effect of modifying the tables inside `definitions`.

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +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;
4344
use rustc_hir::{LangItem, attrs as attr, find_attr};
4445
use rustc_index::IndexVec;
4546
use rustc_index::bit_set::BitMatrix;
@@ -223,6 +224,8 @@ pub struct ResolverAstLowering<'tcx> {
223224

224225
// Information about delegations which is used when handling recursive delegations
225226
pub delegation_infos: LocalDefIdMap<DelegationInfo>,
227+
228+
pub per_parent_disambiguators: LocalDefIdMap<Steal<PerParentDisambiguatorState>>,
226229
}
227230

228231
#[derive(Debug)]

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,6 @@ fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {
21722172

21732173
DefPathData::ValueNs(..)
21742174
| DefPathData::AnonConst
2175-
| DefPathData::LateAnonConst
21762175
| DefPathData::Closure
21772176
| DefPathData::Ctor => Namespace::ValueNS,
21782177

compiler/rustc_resolve/src/lib.rs

Lines changed: 15 additions & 3 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::DisambiguatorState;
60+
use rustc_hir::definitions::PerParentDisambiguatorState;
6161
use rustc_hir::{PrimTy, TraitCandidate, find_attr};
6262
use rustc_index::bit_set::DenseBitSet;
6363
use rustc_metadata::creader::CStore;
@@ -1356,7 +1356,7 @@ pub struct Resolver<'ra, 'tcx> {
13561356

13571357
node_id_to_def_id: NodeMap<Feed<'tcx, LocalDefId>>,
13581358

1359-
disambiguator: DisambiguatorState = DisambiguatorState::new(),
1359+
per_parent_disambiguators: LocalDefIdMap<PerParentDisambiguatorState>,
13601360

13611361
/// Indices of unnamed struct or variant fields with unresolved attributes.
13621362
placeholder_field_indices: FxHashMap<NodeId, usize> = default::fx_hash_map(),
@@ -1560,7 +1560,13 @@ impl<'tcx> Resolver<'_, 'tcx> {
15601560
);
15611561

15621562
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
1563-
let feed = self.tcx.create_def(parent, name, def_kind, None, &mut self.disambiguator);
1563+
let feed = self.tcx.create_def(
1564+
parent,
1565+
name,
1566+
def_kind,
1567+
None,
1568+
self.per_parent_disambiguators.entry(parent).or_default(),
1569+
);
15641570
let def_id = feed.def_id();
15651571

15661572
// Create the definition.
@@ -1742,6 +1748,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17421748
doc_link_resolutions: Default::default(),
17431749
doc_link_traits_in_scope: Default::default(),
17441750
current_crate_outer_attr_insert_span,
1751+
per_parent_disambiguators: Default::default(),
17451752
..
17461753
};
17471754

@@ -1873,6 +1880,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18731880
lifetime_elision_allowed: self.lifetime_elision_allowed,
18741881
lint_buffer: Steal::new(self.lint_buffer),
18751882
delegation_infos: self.delegation_infos,
1883+
per_parent_disambiguators: self
1884+
.per_parent_disambiguators
1885+
.into_items()
1886+
.map(|(k, d)| (k, Steal::new(d)))
1887+
.collect(),
18761888
};
18771889
ResolverOutputs { global_ctxt, ast_lowering }
18781890
}

0 commit comments

Comments
 (0)