Skip to content

Commit c20a97d

Browse files
authored
Rollup merge of #72750 - marmeladema:remove-as-local-node-id, r=petrochenkov
Remove remaining calls to `as_local_node_id` Split out from #72552 cc #50928
2 parents 990195f + 2f3dd7b commit c20a97d

File tree

6 files changed

+50
-43
lines changed

6 files changed

+50
-43
lines changed

src/librustc_ast_lowering/item.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
13211321
.get_partial_res(bound_pred.bounded_ty.id)
13221322
.map(|d| d.base_res())
13231323
{
1324-
if let Some(node_id) =
1325-
self.resolver.definitions().as_local_node_id(def_id)
1326-
{
1324+
if let Some(def_id) = def_id.as_local() {
13271325
for param in &generics.params {
13281326
if let GenericParamKind::Type { .. } = param.kind {
1329-
if node_id == param.id {
1327+
if def_id
1328+
== self
1329+
.resolver
1330+
.definitions()
1331+
.local_def_id(param.id)
1332+
{
13301333
add_bounds
13311334
.entry(param.id)
13321335
.or_default()

src/librustc_hir/definitions.rs

-11
Original file line numberDiff line numberDiff line change
@@ -332,17 +332,6 @@ impl Definitions {
332332
})
333333
}
334334

335-
#[inline]
336-
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
337-
if let Some(def_id) = def_id.as_local() {
338-
let node_id = self.def_id_to_node_id[def_id];
339-
if node_id != ast::DUMMY_NODE_ID {
340-
return Some(node_id);
341-
}
342-
}
343-
None
344-
}
345-
346335
#[inline]
347336
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId {
348337
self.local_def_id_to_hir_id(def_id)

src/librustc_resolve/build_reduced_graph.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_errors::{struct_span_err, Applicability};
2525
use rustc_expand::base::SyntaxExtension;
2626
use rustc_expand::expand::AstFragment;
2727
use rustc_hir::def::{self, *};
28-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
28+
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
2929
use rustc_metadata::creader::LoadedMacro;
3030
use rustc_middle::bug;
3131
use rustc_middle::hir::exports::Export;
@@ -1150,31 +1150,37 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11501150
// Mark the given macro as unused unless its name starts with `_`.
11511151
// Macro uses will remove items from this set, and the remaining
11521152
// items will be reported as `unused_macros`.
1153-
fn insert_unused_macro(&mut self, ident: Ident, node_id: NodeId, span: Span) {
1153+
fn insert_unused_macro(
1154+
&mut self,
1155+
ident: Ident,
1156+
def_id: LocalDefId,
1157+
node_id: NodeId,
1158+
span: Span,
1159+
) {
11541160
if !ident.as_str().starts_with('_') {
1155-
self.r.unused_macros.insert(node_id, span);
1161+
self.r.unused_macros.insert(def_id, (node_id, span));
11561162
}
11571163
}
11581164

11591165
fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScope<'a> {
11601166
let parent_scope = self.parent_scope;
11611167
let expansion = parent_scope.expansion;
1168+
let def_id = self.r.definitions.local_def_id(item.id);
11621169
let (ext, ident, span, macro_rules) = match &item.kind {
11631170
ItemKind::MacroDef(def) => {
11641171
let ext = Lrc::new(self.r.compile_macro(item, self.r.session.edition()));
11651172
(ext, item.ident, item.span, def.macro_rules)
11661173
}
11671174
ItemKind::Fn(..) => match Self::proc_macro_stub(item) {
11681175
Some((macro_kind, ident, span)) => {
1169-
self.r.proc_macro_stubs.insert(item.id);
1176+
self.r.proc_macro_stubs.insert(def_id);
11701177
(self.r.dummy_ext(macro_kind), ident, span, false)
11711178
}
11721179
None => return parent_scope.macro_rules,
11731180
},
11741181
_ => unreachable!(),
11751182
};
11761183

1177-
let def_id = self.r.definitions.local_def_id(item.id);
11781184
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id());
11791185
self.r.macro_map.insert(def_id.to_def_id(), ext);
11801186
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
@@ -1196,7 +1202,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11961202
self.r.define(module, ident, MacroNS, (res, vis, span, expansion, IsMacroExport));
11971203
} else {
11981204
self.r.check_reserved_macro_name(ident, res);
1199-
self.insert_unused_macro(ident, item.id, span);
1205+
self.insert_unused_macro(ident, def_id, item.id, span);
12001206
}
12011207
MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
12021208
parent_macro_rules_scope: parent_scope.macro_rules,
@@ -1214,7 +1220,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12141220
_ => self.resolve_visibility(&item.vis),
12151221
};
12161222
if vis != ty::Visibility::Public {
1217-
self.insert_unused_macro(ident, item.id, span);
1223+
self.insert_unused_macro(ident, def_id, item.id, span);
12181224
}
12191225
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
12201226
self.parent_scope.macro_rules

src/librustc_resolve/late.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1621,11 +1621,10 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16211621
let report_errors = |this: &mut Self, res: Option<Res>| {
16221622
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);
16231623
let def_id = this.parent_scope.module.normal_ancestor_id;
1624-
let node_id = this.r.definitions.as_local_node_id(def_id).unwrap();
16251624
let better = res.is_some();
16261625
let suggestion =
16271626
if res.is_none() { this.report_missing_type_error(path) } else { None };
1628-
this.r.use_injections.push(UseError { err, candidates, node_id, better, suggestion });
1627+
this.r.use_injections.push(UseError { err, candidates, def_id, better, suggestion });
16291628
PartialRes::new(Res::Err)
16301629
};
16311630

src/librustc_resolve/lib.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_ast::ast::{self, FloatTy, IntTy, NodeId, UintTy};
2323
use rustc_ast::ast::{Crate, CRATE_NODE_ID};
2424
use rustc_ast::ast::{ItemKind, Path};
2525
use rustc_ast::attr;
26-
use rustc_ast::node_id::{NodeMap, NodeSet};
26+
use rustc_ast::node_id::NodeMap;
2727
use rustc_ast::unwrap_or;
2828
use rustc_ast::visit::{self, Visitor};
2929
use rustc_ast_pretty::pprust;
@@ -253,21 +253,31 @@ impl<'a> From<&'a ast::PathSegment> for Segment {
253253
}
254254
}
255255

256-
struct UsePlacementFinder {
257-
target_module: NodeId,
256+
struct UsePlacementFinder<'d> {
257+
definitions: &'d Definitions,
258+
target_module: LocalDefId,
258259
span: Option<Span>,
259260
found_use: bool,
260261
}
261262

262-
impl UsePlacementFinder {
263-
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
264-
let mut finder = UsePlacementFinder { target_module, span: None, found_use: false };
265-
visit::walk_crate(&mut finder, krate);
266-
(finder.span, finder.found_use)
263+
impl<'d> UsePlacementFinder<'d> {
264+
fn check(
265+
definitions: &'d Definitions,
266+
krate: &Crate,
267+
target_module: DefId,
268+
) -> (Option<Span>, bool) {
269+
if let Some(target_module) = target_module.as_local() {
270+
let mut finder =
271+
UsePlacementFinder { definitions, target_module, span: None, found_use: false };
272+
visit::walk_crate(&mut finder, krate);
273+
(finder.span, finder.found_use)
274+
} else {
275+
(None, false)
276+
}
267277
}
268278
}
269279

270-
impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
280+
impl<'tcx, 'd> Visitor<'tcx> for UsePlacementFinder<'d> {
271281
fn visit_mod(
272282
&mut self,
273283
module: &'tcx ast::Mod,
@@ -278,7 +288,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
278288
if self.span.is_some() {
279289
return;
280290
}
281-
if node_id != self.target_module {
291+
if self.definitions.local_def_id(node_id) != self.target_module {
282292
visit::walk_mod(self, module);
283293
return;
284294
}
@@ -611,7 +621,7 @@ struct UseError<'a> {
611621
/// Attach `use` statements for these candidates.
612622
candidates: Vec<ImportSuggestion>,
613623
/// The `NodeId` of the module to place the use-statements in.
614-
node_id: NodeId,
624+
def_id: DefId,
615625
/// Whether the diagnostic should state that it's "better".
616626
better: bool,
617627
/// Extra free form suggestion. Currently used to suggest new type parameter.
@@ -926,8 +936,8 @@ pub struct Resolver<'a> {
926936
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
927937
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
928938
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
929-
unused_macros: NodeMap<Span>,
930-
proc_macro_stubs: NodeSet,
939+
unused_macros: FxHashMap<LocalDefId, (NodeId, Span)>,
940+
proc_macro_stubs: FxHashSet<LocalDefId>,
931941
/// Traces collected during macro resolution and validated when it's complete.
932942
single_segment_macro_resolutions:
933943
Vec<(Ident, MacroKind, ParentScope<'a>, Option<&'a NameBinding<'a>>)>,
@@ -2567,10 +2577,10 @@ impl<'a> Resolver<'a> {
25672577
}
25682578

25692579
fn report_with_use_injections(&mut self, krate: &Crate) {
2570-
for UseError { mut err, candidates, node_id, better, suggestion } in
2580+
for UseError { mut err, candidates, def_id, better, suggestion } in
25712581
self.use_injections.drain(..)
25722582
{
2573-
let (span, found_use) = UsePlacementFinder::check(krate, node_id);
2583+
let (span, found_use) = UsePlacementFinder::check(&self.definitions, krate, def_id);
25742584
if !candidates.is_empty() {
25752585
diagnostics::show_candidates(&mut err, span, &candidates, better, found_use);
25762586
} else if let Some((span, msg, sugg, appl)) = suggestion {

src/librustc_resolve/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a> base::Resolver for Resolver<'a> {
333333
}
334334

335335
fn check_unused_macros(&mut self) {
336-
for (&node_id, &span) in self.unused_macros.iter() {
336+
for (_, &(node_id, span)) in self.unused_macros.iter() {
337337
self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition");
338338
}
339339
}
@@ -416,9 +416,9 @@ impl<'a> Resolver<'a> {
416416

417417
match res {
418418
Res::Def(DefKind::Macro(_), def_id) => {
419-
if let Some(node_id) = self.definitions.as_local_node_id(def_id) {
420-
self.unused_macros.remove(&node_id);
421-
if self.proc_macro_stubs.contains(&node_id) {
419+
if let Some(def_id) = def_id.as_local() {
420+
self.unused_macros.remove(&def_id);
421+
if self.proc_macro_stubs.contains(&def_id) {
422422
self.session.span_err(
423423
path.span,
424424
"can't use a procedural macro from the same crate that defines it",

0 commit comments

Comments
 (0)