Skip to content

Commit e4ed73e

Browse files
Rollup merge of rust-lang#156135 - oli-obk:def_id_to_node_id_reduction, r=petrochenkov
Remove most uses of def_id_to_node_id Especially those happening during normal resolving, where we are actually looking at current parent scopes. It allows follow-up refactorings to not have a way to go from `NodeId` to `DefId` until macro expansion is done r? @petrochenkov
2 parents 145df99 + fd701b7 commit e4ed73e

6 files changed

Lines changed: 65 additions & 48 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use std::sync::Arc;
99

1010
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1111
use rustc_ast::{
12-
self as ast, AssocItem, AssocItemKind, Block, ConstItem, Delegation, Fn, ForeignItem,
13-
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias, TyAlias,
12+
self as ast, AssocItem, AssocItemKind, Block, ConstItem, DUMMY_NODE_ID, Delegation, Fn,
13+
ForeignItem, ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias,
14+
TyAlias,
1415
};
1516
use rustc_attr_parsing::AttributeParser;
1617
use rustc_expand::base::ResolverExpand;
@@ -168,7 +169,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
168169
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
169170
let module = self.new_extern_module(
170171
parent,
171-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
172+
ModuleKind::Def(
173+
def_kind,
174+
def_id,
175+
DUMMY_NODE_ID,
176+
Some(self.tcx.item_name(def_id)),
177+
),
172178
expn_id,
173179
self.def_span(def_id),
174180
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -251,7 +257,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
251257
// Any inherited visibility resolved directly inside an enum or trait
252258
// (i.e. variants, fields, and trait items) inherits from the visibility
253259
// of the enum or trait.
254-
ModuleKind::Def(DefKind::Enum | DefKind::Trait, def_id, _) => {
260+
ModuleKind::Def(DefKind::Enum | DefKind::Trait, def_id, _, _) => {
255261
self.tcx.visibility(def_id).expect_local()
256262
}
257263
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
@@ -848,7 +854,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
848854
}
849855
let module = self.r.new_local_module(
850856
Some(parent),
851-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
857+
ModuleKind::Def(def_kind, def_id, item.id, Some(ident.name)),
852858
expansion.to_expn_id(),
853859
item.span,
854860
parent.no_implicit_prelude
@@ -882,7 +888,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
882888

883889
let module = self.r.new_local_module(
884890
Some(parent),
885-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
891+
ModuleKind::Def(def_kind, def_id, item.id, Some(ident.name)),
886892
expansion.to_expn_id(),
887893
item.span,
888894
parent.no_implicit_prelude,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::ops::ControlFlow;
55
use itertools::Itertools as _;
66
use rustc_ast::visit::{self, Visitor};
77
use rustc_ast::{
8-
self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents,
8+
self as ast, CRATE_NODE_ID, Crate, DUMMY_NODE_ID, ItemKind, ModKind, NodeId, Path,
9+
join_path_idents,
910
};
1011
use rustc_ast_pretty::pprust;
1112
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -192,11 +193,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
192193
}
193194

194195
fn report_with_use_injections(&mut self, krate: &Crate) {
195-
for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
196+
for UseError { mut err, candidates, node_id, instead, suggestion, path, is_call } in
196197
mem::take(&mut self.use_injections)
197198
{
198-
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
199-
UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id))
199+
let (span, found_use) = if node_id != DUMMY_NODE_ID {
200+
UsePlacementFinder::check(krate, node_id)
200201
} else {
201202
(None, FoundUse::No)
202203
};
@@ -242,7 +243,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
242243
let container = match old_binding.parent_module.unwrap().kind {
243244
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
244245
// indirectly *calls* the resolver, and would cause a query cycle.
245-
ModuleKind::Def(kind, def_id, _) => kind.descr(def_id),
246+
ModuleKind::Def(kind, def_id, _, _) => kind.descr(def_id),
246247
ModuleKind::Block => "block",
247248
};
248249

@@ -1705,9 +1706,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17051706

17061707
let import_suggestions =
17071708
self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
1708-
let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() {
1709-
Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)),
1710-
None => (None, FoundUse::No),
1709+
let (span, found_use) = match parent_scope.module.nearest_parent_mod_node_id() {
1710+
DUMMY_NODE_ID => (None, FoundUse::No),
1711+
node_id => UsePlacementFinder::check(krate, node_id),
17111712
};
17121713
show_candidates(
17131714
self.tcx,
@@ -1764,7 +1765,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17641765
}
17651766

17661767
if ident.name == kw::Default
1767-
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1768+
&& let ModuleKind::Def(DefKind::Enum, def_id, _, _) = parent_scope.module.kind
17681769
{
17691770
let span = self.def_span(def_id);
17701771
let source_map = self.tcx.sess.source_map();
@@ -1892,19 +1893,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18921893
missing a `derive` attribute",
18931894
ident.name,
18941895
);
1895-
let sugg_span = if let ModuleKind::Def(DefKind::Enum, id, _) = parent_scope.module.kind
1896-
{
1897-
let span = self.def_span(id);
1898-
if span.from_expansion() {
1899-
None
1896+
let sugg_span =
1897+
if let ModuleKind::Def(DefKind::Enum, id, _, _) = parent_scope.module.kind {
1898+
let span = self.def_span(id);
1899+
if span.from_expansion() {
1900+
None
1901+
} else {
1902+
// For enum variants sugg_span is empty but we can get the enum's Span.
1903+
Some(span.shrink_to_lo())
1904+
}
19001905
} else {
1901-
// For enum variants sugg_span is empty but we can get the enum's Span.
1902-
Some(span.shrink_to_lo())
1903-
}
1904-
} else {
1905-
// For items this `Span` will be populated, everything else it'll be None.
1906-
sugg_span
1907-
};
1906+
// For items this `Span` will be populated, everything else it'll be None.
1907+
sugg_span
1908+
};
19081909
match sugg_span {
19091910
Some(span) => {
19101911
err.span_suggestion_verbose(

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
644644
}
645645
}
646646
Scope::ModuleGlobs(module, _)
647-
if let ModuleKind::Def(_, def_id, _) = module.kind
647+
if let ModuleKind::Def(_, def_id, _, _) = module.kind
648648
&& !def_id.is_local() =>
649649
{
650650
// Fast path: external module decoding only creates non-glob declarations.

compiler/rustc_resolve/src/late.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
19411941
&& let Some((module, _)) = &self.current_trait_ref
19421942
&& let Some(ty) = &self.diag_metadata.current_self_type
19431943
&& Some(true) == self.diag_metadata.in_non_gat_assoc_type
1944-
&& let crate::ModuleKind::Def(DefKind::Trait, trait_id, _) = module.kind
1944+
&& let crate::ModuleKind::Def(DefKind::Trait, trait_id, _, _) =
1945+
module.kind
19451946
{
19461947
if def_id_matches_path(
19471948
self.r.tcx,
@@ -4514,7 +4515,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
45144515
parent_qself,
45154516
);
45164517

4517-
let def_id = this.parent_scope.module.nearest_parent_mod();
4518+
let node_id = this.parent_scope.module.nearest_parent_mod_node_id();
45184519
let instead = res.is_some();
45194520
let (suggestion, const_err) = if let Some((start, end)) =
45204521
this.diag_metadata.in_range
@@ -4556,7 +4557,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
45564557
let ue = UseError {
45574558
err,
45584559
candidates,
4559-
def_id,
4560+
node_id,
45604561
instead,
45614562
suggestion,
45624563
path: path.into(),
@@ -4645,7 +4646,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46454646

46464647
parent_err.cancel();
46474648

4648-
let def_id = this.parent_scope.module.nearest_parent_mod();
4649+
let node_id = this.parent_scope.module.nearest_parent_mod_node_id();
46494650

46504651
if this.should_report_errs() {
46514652
if candidates.is_empty() {
@@ -4670,7 +4671,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46704671
this.r.use_injections.push(UseError {
46714672
err,
46724673
candidates,
4673-
def_id,
4674+
node_id,
46744675
instead: false,
46754676
suggestion: None,
46764677
path: prefix_path.into(),

compiler/rustc_resolve/src/lib.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ enum ModuleKind {
543543
/// The crate root will have `None` for the symbol.
544544
/// * A trait or an enum (it implicitly contains associated types, methods and variant
545545
/// constructors).
546-
Def(DefKind, DefId, Option<Symbol>),
546+
Def(DefKind, DefId, NodeId, Option<Symbol>),
547547
}
548548

549549
impl ModuleKind {
@@ -557,7 +557,7 @@ impl ModuleKind {
557557

558558
fn opt_def_id(&self) -> Option<DefId> {
559559
match self {
560-
ModuleKind::Def(_, def_id, _) => Some(*def_id),
560+
ModuleKind::Def(_, def_id, _, _) => Some(*def_id),
561561
_ => None,
562562
}
563563
}
@@ -726,7 +726,7 @@ impl<'ra> ModuleData<'ra> {
726726
self_decl: Option<Decl<'ra>>,
727727
) -> Self {
728728
let is_foreign = match kind {
729-
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
729+
ModuleKind::Def(_, def_id, _, _) => !def_id.is_local(),
730730
ModuleKind::Block => false,
731731
};
732732
ModuleData {
@@ -756,7 +756,7 @@ impl<'ra> ModuleData<'ra> {
756756

757757
fn res(&self) -> Option<Res> {
758758
match self.kind {
759-
ModuleKind::Def(kind, def_id, _) => Some(Res::Def(kind, def_id)),
759+
ModuleKind::Def(kind, def_id, _, _) => Some(Res::Def(kind, def_id)),
760760
_ => None,
761761
}
762762
}
@@ -813,11 +813,11 @@ impl<'ra> Module<'ra> {
813813

814814
// `self` resolves to the first module ancestor that `is_normal`.
815815
fn is_normal(self) -> bool {
816-
matches!(self.kind, ModuleKind::Def(DefKind::Mod, _, _))
816+
matches!(self.kind, ModuleKind::Def(DefKind::Mod, _, _, _))
817817
}
818818

819819
fn is_trait(self) -> bool {
820-
matches!(self.kind, ModuleKind::Def(DefKind::Trait, _, _))
820+
matches!(self.kind, ModuleKind::Def(DefKind::Trait, _, _, _))
821821
}
822822

823823
fn nearest_item_scope(self) -> Module<'ra> {
@@ -833,11 +833,20 @@ impl<'ra> Module<'ra> {
833833
/// This may be the crate root.
834834
fn nearest_parent_mod(self) -> DefId {
835835
match self.kind {
836-
ModuleKind::Def(DefKind::Mod, def_id, _) => def_id,
836+
ModuleKind::Def(DefKind::Mod, def_id, _, _) => def_id,
837837
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
838838
}
839839
}
840840

841+
/// The [`NodeId`] of the nearest `mod` item ancestor (which may be this module).
842+
/// This may be the crate root.
843+
fn nearest_parent_mod_node_id(self) -> NodeId {
844+
match self.kind {
845+
ModuleKind::Def(DefKind::Mod, _, node_id, _) => node_id,
846+
_ => self.parent.expect("non-root module without parent").nearest_parent_mod_node_id(),
847+
}
848+
}
849+
841850
fn is_ancestor_of(self, mut other: Self) -> bool {
842851
while self != other {
843852
if let Some(parent) = other.parent {
@@ -852,7 +861,7 @@ impl<'ra> Module<'ra> {
852861
#[track_caller]
853862
fn expect_local(self) -> LocalModule<'ra> {
854863
match self.kind {
855-
ModuleKind::Def(_, def_id, _) if !def_id.is_local() => {
864+
ModuleKind::Def(_, def_id, _, _) if !def_id.is_local() => {
856865
panic!("`Module::expect_local` is called on a non-local module: {self:?}")
857866
}
858867
ModuleKind::Def(..) | ModuleKind::Block => LocalModule(self.0),
@@ -862,7 +871,7 @@ impl<'ra> Module<'ra> {
862871
#[track_caller]
863872
fn expect_extern(self) -> ExternModule<'ra> {
864873
match self.kind {
865-
ModuleKind::Def(_, def_id, _) if !def_id.is_local() => ExternModule(self.0),
874+
ModuleKind::Def(_, def_id, _, _) if !def_id.is_local() => ExternModule(self.0),
866875
ModuleKind::Def(..) | ModuleKind::Block => {
867876
panic!("`Module::expect_extern` is called on a local module: {self:?}")
868877
}
@@ -992,8 +1001,8 @@ struct UseError<'a> {
9921001
err: Diag<'a>,
9931002
/// Candidates which user could `use` to access the missing type.
9941003
candidates: Vec<ImportSuggestion>,
995-
/// The `DefId` of the module to place the use-statements in.
996-
def_id: DefId,
1004+
/// The `NodeId` of the module to place the use-statements in.
1005+
node_id: NodeId,
9971006
/// Whether the diagnostic should say "instead" (as in `consider importing ... instead`).
9981007
instead: bool,
9991008
/// Extra free-form suggestion.
@@ -1551,7 +1560,7 @@ impl<'ra> ResolverArenas<'ra> {
15511560
no_implicit_prelude: bool,
15521561
) -> Module<'ra> {
15531562
let self_decl = match kind {
1554-
ModuleKind::Def(def_kind, def_id, _) => Some(self.new_def_decl(
1563+
ModuleKind::Def(def_kind, def_id, _, _) => Some(self.new_def_decl(
15551564
Res::Def(def_kind, def_id),
15561565
vis,
15571566
span,
@@ -1732,7 +1741,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17321741
let root_def_id = CRATE_DEF_ID.to_def_id();
17331742
let graph_root = arenas.new_module(
17341743
None,
1735-
ModuleKind::Def(DefKind::Mod, root_def_id, None),
1744+
ModuleKind::Def(DefKind::Mod, root_def_id, CRATE_NODE_ID, None),
17361745
Visibility::Public,
17371746
ExpnId::root(),
17381747
crate_span,
@@ -1743,7 +1752,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17431752
let local_module_map = FxIndexMap::from_iter([(CRATE_DEF_ID, graph_root)]);
17441753
let empty_module = arenas.new_module(
17451754
None,
1746-
ModuleKind::Def(DefKind::Mod, root_def_id, None),
1755+
ModuleKind::Def(DefKind::Mod, root_def_id, CRATE_NODE_ID, None),
17471756
Visibility::Public,
17481757
ExpnId::root(),
17491758
DUMMY_SP,

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11851185
self.get_mut().record_use(ident, fallback_binding, Used::Other);
11861186
} else {
11871187
let location = match parent_scope.module.kind {
1188-
ModuleKind::Def(kind, def_id, name) => {
1188+
ModuleKind::Def(kind, def_id, _, name) => {
11891189
if let Some(name) = name {
11901190
format!("{} `{name}`", kind.descr(def_id))
11911191
} else {

0 commit comments

Comments
 (0)