Skip to content

Commit

Permalink
Move next_disambiguator to Resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Jun 21, 2020
1 parent 1d3f49f commit bd4f6f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ pub struct Definitions {
parent_modules_of_macro_defs: FxHashMap<ExpnId, DefId>,
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
}

/// A unique identifier that we can use to lookup a definition
Expand Down Expand Up @@ -350,7 +349,6 @@ impl Definitions {
def_id_to_hir_id: Default::default(),
hir_id_to_def_id: Default::default(),
expansions_that_defined: Default::default(),
next_disambiguator: Default::default(),
parent_modules_of_macro_defs: Default::default(),
}
}
Expand All @@ -366,20 +364,14 @@ impl Definitions {
parent: LocalDefId,
data: DefPathData,
expn_id: ExpnId,
mut next_disambiguator: impl FnMut(LocalDefId, DefPathData) -> u32,
) -> LocalDefId {
debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);

// The root node must be created with `create_root_def()`.
assert!(data != DefPathData::CrateRoot);

// Find the next free disambiguator for this key.
let disambiguator = {
let next_disamb = self.next_disambiguator.entry((parent, data)).or_insert(0);
let disambiguator = *next_disamb;
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
disambiguator
};

let disambiguator = next_disambiguator(parent, data);
let key = DefKey {
parent: Some(parent.local_def_index),
disambiguated_data: DisambiguatedDefPathData { data, disambiguator },
Expand Down
14 changes: 13 additions & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,8 @@ pub struct Resolver<'a> {
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
/// we know what parent node that fragment should be attached to thanks to this table.
invocation_parents: FxHashMap<ExpnId, LocalDefId>,

next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
}

/// Nothing really interesting here; it just provides memory for the rest of the crate.
Expand Down Expand Up @@ -1142,7 +1144,16 @@ impl ResolverAstLowering for Resolver<'_> {
self.definitions.def_key(self.node_id_to_def_id[&node_id]),
);

let def_id = self.definitions.create_def(parent, data, expn_id);
// Find the next free disambiguator for this key.
let next_disambiguator = &mut self.next_disambiguator;
let next_disambiguator = |parent, data| {
let next_disamb = next_disambiguator.entry((parent, data)).or_insert(0);
let disambiguator = *next_disamb;
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
disambiguator
};

let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator);

assert_eq!(self.def_id_to_span.push(span), def_id);

Expand Down Expand Up @@ -1322,6 +1333,7 @@ impl<'a> Resolver<'a> {
def_id_to_node_id,
placeholder_field_indices: Default::default(),
invocation_parents,
next_disambiguator: Default::default(),
}
}

Expand Down

0 comments on commit bd4f6f0

Please sign in to comment.