From fd701b79a71a93a4df5f60262e249e23aa1648cb Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 May 2026 10:37:46 +0200 Subject: [PATCH] Remove most uses of `def_id_to_node_id` Especially those happening during normal resolving, where we are actually looking at current parent scopes --- compiler/rustc_resolve/src/diagnostics.rs | 15 ++++++++------- compiler/rustc_resolve/src/late.rs | 8 ++++---- compiler/rustc_resolve/src/lib.rs | 4 ++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f891622d0bae..c082455380ce 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -5,7 +5,8 @@ use std::ops::ControlFlow; use itertools::Itertools as _; use rustc_ast::visit::{self, Visitor}; use rustc_ast::{ - self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents, + self as ast, CRATE_NODE_ID, Crate, DUMMY_NODE_ID, ItemKind, ModKind, NodeId, Path, + join_path_idents, }; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -192,11 +193,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } fn report_with_use_injections(&mut self, krate: &Crate) { - for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in + for UseError { mut err, candidates, node_id, instead, suggestion, path, is_call } in mem::take(&mut self.use_injections) { - let (span, found_use) = if let Some(def_id) = def_id.as_local() { - UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)) + let (span, found_use) = if node_id != DUMMY_NODE_ID { + UsePlacementFinder::check(krate, node_id) } else { (None, FoundUse::No) }; @@ -1705,9 +1706,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let import_suggestions = self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); - let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() { - Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)), - None => (None, FoundUse::No), + let (span, found_use) = match parent_scope.module.nearest_parent_mod_node_id() { + DUMMY_NODE_ID => (None, FoundUse::No), + node_id => UsePlacementFinder::check(krate, node_id), }; show_candidates( self.tcx, diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 29a151b277d5..3337a4626b04 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -4515,7 +4515,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { parent_qself, ); - let def_id = this.parent_scope.module.nearest_parent_mod(); + let node_id = this.parent_scope.module.nearest_parent_mod_node_id(); let instead = res.is_some(); let (suggestion, const_err) = if let Some((start, end)) = this.diag_metadata.in_range @@ -4557,7 +4557,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { let ue = UseError { err, candidates, - def_id, + node_id, instead, suggestion, path: path.into(), @@ -4646,7 +4646,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { parent_err.cancel(); - let def_id = this.parent_scope.module.nearest_parent_mod(); + let node_id = this.parent_scope.module.nearest_parent_mod_node_id(); if this.should_report_errs() { if candidates.is_empty() { @@ -4671,7 +4671,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { this.r.use_injections.push(UseError { err, candidates, - def_id, + node_id, instead: false, suggestion: None, path: prefix_path.into(), diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index af9a72b13c1d..f5f063ecaf45 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1001,8 +1001,8 @@ struct UseError<'a> { err: Diag<'a>, /// Candidates which user could `use` to access the missing type. candidates: Vec, - /// The `DefId` of the module to place the use-statements in. - def_id: DefId, + /// The `NodeId` of the module to place the use-statements in. + node_id: NodeId, /// Whether the diagnostic should say "instead" (as in `consider importing ... instead`). instead: bool, /// Extra free-form suggestion.