From 1f5e4badf1a7c318f761f1be618c7b9ec491f00b Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Sat, 11 Jul 2026 21:14:46 -0500 Subject: [PATCH 1/6] Add Into to is_descendant_of --- compiler/rustc_middle/src/ty/mod.rs | 10 +++++++--- compiler/rustc_resolve/src/macros.rs | 2 +- .../src/error_reporting/infer/region.rs | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index c482e6bb87345..6916083071d5a 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -403,9 +403,13 @@ impl TyCtxt<'_> { } } - pub fn is_descendant_of(self, descendant: DefId, ancestor: DefId) -> bool { + pub fn is_descendant_of( + self, + descendant: impl Into, + ancestor: impl Into, + ) -> bool { matches!( - self.def_id_partial_cmp(descendant, ancestor), + self.def_id_partial_cmp(descendant.into(), ancestor.into()), Some(Ordering::Less | Ordering::Equal) ) } @@ -434,7 +438,7 @@ impl> Visibility { match self { // Public items are visible everywhere. Visibility::Public => true, - Visibility::Restricted(id) => tcx.is_descendant_of(module.into(), id.into()), + Visibility::Restricted(id) => tcx.is_descendant_of(module, id), } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index f78b790e2aa87..6f204fd6dfd5f 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -1170,7 +1170,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { && kinds.contains(MacroKinds::BANG) // And the `macro_rules` is defined inside the attribute's module, // so it cannot be in scope unless imported. - && self.tcx.is_descendant_of(def_id, mod_def_id.to_def_id()) + && self.tcx.is_descendant_of(def_id, mod_def_id) { // Try to resolve our ident ignoring `macro_rules` scopes. // If such resolution is successful and gives the same result diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs index 7e706a9838306..e104cc04310be 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs @@ -833,7 +833,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { None => generic_param_scope, }, }; - match self.tcx.is_descendant_of(type_scope.into(), lifetime_scope.into()) { + match self.tcx.is_descendant_of(type_scope, lifetime_scope) { true => type_scope, false => lifetime_scope, } From d26029dbf2294c68700341db3d71601203843c6c Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Sun, 12 Jul 2026 16:41:59 -0500 Subject: [PATCH 2/6] Inline typed_def_id macro --- compiler/rustc_span/src/def_id.rs | 144 ++++++++++++++---------------- 1 file changed, 65 insertions(+), 79 deletions(-) diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 0ea7da4f2ab87..0f85610ab665a 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -465,102 +465,88 @@ impl ToStableHashKey for LocalDefId { } } -macro_rules! typed_def_id { - ($Name:ident, $LocalName:ident) => { - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] - pub struct $Name(DefId); - - impl $Name { - #[inline] - pub const fn new_unchecked(def_id: DefId) -> Self { - Self(def_id) - } - - #[inline] - pub fn to_def_id(self) -> DefId { - self.into() - } - - #[inline] - pub fn is_local(self) -> bool { - self.0.is_local() - } - - #[inline] - pub fn as_local(self) -> Option<$LocalName> { - self.0.as_local().map($LocalName::new_unchecked) - } - } - - impl From<$LocalName> for $Name { - #[inline] - fn from(local: $LocalName) -> Self { - Self(local.0.to_def_id()) - } - } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] +pub struct ModDefId(DefId); - impl From<$Name> for DefId { - #[inline] - fn from(typed: $Name) -> Self { - typed.0 - } - } - - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] - pub struct $LocalName(LocalDefId); +impl ModDefId { + #[inline] + pub const fn new_unchecked(def_id: DefId) -> Self { + Self(def_id) + } - impl !Ord for $LocalName {} - impl !PartialOrd for $LocalName {} + #[inline] + pub fn to_def_id(self) -> DefId { + self.into() + } - impl $LocalName { - #[inline] - pub const fn new_unchecked(def_id: LocalDefId) -> Self { - Self(def_id) - } + #[inline] + pub fn is_local(self) -> bool { + self.0.is_local() + } - #[inline] - pub fn to_def_id(self) -> DefId { - self.0.into() - } + #[inline] + pub fn as_local(self) -> Option { + self.0.as_local().map(LocalModDefId::new_unchecked) + } - #[inline] - pub fn to_local_def_id(self) -> LocalDefId { - self.0 - } - } + pub fn is_top_level_module(self) -> bool { + self.0.is_top_level_module() + } +} - impl From<$LocalName> for LocalDefId { - #[inline] - fn from(typed: $LocalName) -> Self { - typed.0 - } - } +impl From for ModDefId { + #[inline] + fn from(local: LocalModDefId) -> Self { + Self(local.0.to_def_id()) + } +} - impl From<$LocalName> for DefId { - #[inline] - fn from(typed: $LocalName) -> Self { - typed.0.into() - } - } - }; +impl From for DefId { + #[inline] + fn from(typed: ModDefId) -> Self { + typed.0 + } } -// N.B.: when adding new typed `DefId`s update the corresponding trait impls in -// `rustc_middle::dep_graph::dep_node_key` for `DepNodeKey`. -typed_def_id! { ModDefId, LocalModDefId } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] +pub struct LocalModDefId(LocalDefId); + +impl !Ord for LocalModDefId {} +impl !PartialOrd for LocalModDefId {} impl LocalModDefId { pub const CRATE_DEF_ID: Self = Self::new_unchecked(CRATE_DEF_ID); -} -impl ModDefId { + #[inline] + pub const fn new_unchecked(def_id: LocalDefId) -> Self { + Self(def_id) + } + pub fn is_top_level_module(self) -> bool { self.0.is_top_level_module() } + + #[inline] + pub fn to_def_id(self) -> DefId { + self.0.into() + } + + #[inline] + pub fn to_local_def_id(self) -> LocalDefId { + self.0 + } } -impl LocalModDefId { - pub fn is_top_level_module(self) -> bool { - self.0.is_top_level_module() +impl From for LocalDefId { + #[inline] + fn from(typed: LocalModDefId) -> Self { + typed.0 + } +} + +impl From for DefId { + #[inline] + fn from(typed: LocalModDefId) -> Self { + typed.0.into() } } From df3766de5113ae5ed9ab157bcf67747a102355d7 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Sun, 12 Jul 2026 17:29:20 -0500 Subject: [PATCH 3/6] Rename ModDefId to ModId --- compiler/rustc_lint/src/late.rs | 14 +++---- compiler/rustc_lint/src/lib.rs | 6 +-- .../src/dep_graph/dep_node_key.rs | 10 ++--- compiler/rustc_middle/src/hir/map.rs | 30 +++++++------- compiler/rustc_middle/src/hir/mod.rs | 10 ++--- compiler/rustc_middle/src/queries.rs | 16 ++++---- .../rustc_middle/src/query/into_query_key.rs | 8 ++-- compiler/rustc_middle/src/query/keys.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 10 ++--- compiler/rustc_middle/src/ty/trait_def.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 4 +- compiler/rustc_passes/src/dead.rs | 6 +-- compiler/rustc_passes/src/stability.rs | 12 +++--- compiler/rustc_privacy/src/lib.rs | 22 +++++------ compiler/rustc_span/src/def_id.rs | 39 +++++++++---------- 15 files changed, 94 insertions(+), 99 deletions(-) diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index c17fe6f2e510f..5e4ca337f8042 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -8,7 +8,7 @@ use std::cell::Cell; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::sync::par_join; -use rustc_hir::def_id::{LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{LocalDefId, LocalModId}; use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; @@ -335,7 +335,7 @@ crate::late_lint_methods!(impl_late_lint_pass, []); pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( tcx: TyCtxt<'tcx>, - module_def_id: LocalModDefId, + mod_id: LocalModId, builtin_lints: T, ) { let context = LateContext { @@ -344,7 +344,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( cached_typeck_results: Cell::new(None), param_env: ty::ParamEnv::empty(), effective_visibilities: tcx.effective_visibilities(()), - last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id), + last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(mod_id), generics: None, only_module: true, }; @@ -363,26 +363,26 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>( let builtin_lints_must_run = is_lint_pass_required(skippable_lints, &builtin_lints.get_lints()); if passes.is_empty() { if builtin_lints_must_run { - late_lint_mod_inner(tcx, module_def_id, context, builtin_lints); + late_lint_mod_inner(tcx, mod_id, context, builtin_lints); } } else { if builtin_lints_must_run { passes.push(Box::new(builtin_lints) as Box>); } let pass = RuntimeCombinedLateLintPass { passes }; - late_lint_mod_inner(tcx, module_def_id, context, pass); + late_lint_mod_inner(tcx, mod_id, context, pass); } } fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>( tcx: TyCtxt<'tcx>, - module_def_id: LocalModDefId, + mod_id: LocalModId, context: LateContext<'tcx>, pass: T, ) { let mut cx = LateContextAndPass { context, pass }; - let (module, _span, hir_id) = tcx.hir_get_module(module_def_id); + let (module, _span, hir_id) = tcx.hir_get_module(mod_id); cx.with_lint_attrs(hir_id, |cx| { // There is no module lint that will have the crate itself as an item, so check it here. diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index eaf9360cc3358..5271217593e62 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -121,7 +121,7 @@ use redundant_semicolon::*; use reference_casting::*; use runtime_symbols::*; use rustc_data_structures::unord::UnordSet; -use rustc_hir::def_id::LocalModDefId; +use rustc_hir::def_id::LocalModId; use rustc_middle::query::Providers; use rustc_middle::ty::TyCtxt; use shadowed_into_iter::ShadowedIntoIter; @@ -154,8 +154,8 @@ pub fn provide(providers: &mut Providers) { *providers = Providers { lint_mod, ..*providers }; } -fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { - late_lint_mod(tcx, module_def_id, BuiltinCombinedLateLintModPass::new()); +fn lint_mod(tcx: TyCtxt<'_>, mod_id: LocalModId) { + late_lint_mod(tcx, mod_id, BuiltinCombinedLateLintModPass::new()); } early_lint_methods!( diff --git a/compiler/rustc_middle/src/dep_graph/dep_node_key.rs b/compiler/rustc_middle/src/dep_graph/dep_node_key.rs index 618b061442d43..870864da2714b 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node_key.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node_key.rs @@ -2,7 +2,7 @@ use std::fmt::Debug; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hash::{StableHash, StableHasher}; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId}; +use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModId, ModId}; use rustc_hir::definitions::DefPathHash; use rustc_hir::{HirId, ItemLocalId, OwnerId}; @@ -194,7 +194,7 @@ impl<'tcx> DepNodeKey<'tcx> for HirId { } } -impl<'tcx> DepNodeKey<'tcx> for ModDefId { +impl<'tcx> DepNodeKey<'tcx> for ModId { #[inline(always)] fn key_fingerprint_style() -> KeyFingerprintStyle { KeyFingerprintStyle::DefPathHash @@ -207,11 +207,11 @@ impl<'tcx> DepNodeKey<'tcx> for ModDefId { #[inline(always)] fn try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - DefId::try_recover_key(tcx, dep_node).map(ModDefId::new_unchecked) + DefId::try_recover_key(tcx, dep_node).map(ModId::new_unchecked) } } -impl<'tcx> DepNodeKey<'tcx> for LocalModDefId { +impl<'tcx> DepNodeKey<'tcx> for LocalModId { #[inline(always)] fn key_fingerprint_style() -> KeyFingerprintStyle { KeyFingerprintStyle::DefPathHash @@ -224,6 +224,6 @@ impl<'tcx> DepNodeKey<'tcx> for LocalModDefId { #[inline(always)] fn try_recover_key(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - LocalDefId::try_recover_key(tcx, dep_node).map(LocalModDefId::new_unchecked) + LocalDefId::try_recover_key(tcx, dep_node).map(LocalModId::new_unchecked) } } diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 82c0503d5468b..b0abc01af9d17 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -10,13 +10,13 @@ use rustc_data_structures::steal::Steal; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in}; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModId}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_hir::intravisit::Visitor; use rustc_hir::lints::DelayedLints; use rustc_hir::*; use rustc_hir_pretty as pprust_hir; -use rustc_span::def_id::StableCrateId; +use rustc_span::def_id::{CRATE_MOD_ID, StableCrateId}; use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans}; use crate::hir::{ModuleItems, ProjectedMaybeOwner, nested_filter}; @@ -207,7 +207,7 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] - pub fn hir_module_free_items(self, module: LocalModDefId) -> impl Iterator { + pub fn hir_module_free_items(self, module: LocalModId) -> impl Iterator { self.hir_module_items(module).free_items() } @@ -412,7 +412,7 @@ impl<'tcx> TyCtxt<'tcx> { find_attr!(self.hir_krate_attrs(), RustcCoherenceIsCore) } - pub fn hir_get_module(self, module: LocalModDefId) -> (&'tcx Mod<'tcx>, Span, HirId) { + pub fn hir_get_module(self, module: LocalModId) -> (&'tcx Mod<'tcx>, Span, HirId) { let hir_id = HirId::make_owner(module.to_local_def_id()); match self.hir_owner_node(hir_id.owner) { OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id), @@ -426,7 +426,7 @@ impl<'tcx> TyCtxt<'tcx> { where V: Visitor<'tcx>, { - let (top_mod, span, hir_id) = self.hir_get_module(LocalModDefId::CRATE_DEF_ID); + let (top_mod, span, hir_id) = self.hir_get_module(CRATE_MOD_ID); visitor.visit_mod(top_mod, span, hir_id) } @@ -477,11 +477,7 @@ impl<'tcx> TyCtxt<'tcx> { /// This method is the equivalent of `visit_all_item_likes_in_crate` but restricted to /// item-likes in a single module. - pub fn hir_visit_item_likes_in_module( - self, - module: LocalModDefId, - visitor: &mut V, - ) -> V::Result + pub fn hir_visit_item_likes_in_module(self, module: LocalModId, visitor: &mut V) -> V::Result where V: Visitor<'tcx>, { @@ -501,29 +497,29 @@ impl<'tcx> TyCtxt<'tcx> { V::Result::output() } - pub fn hir_for_each_module(self, mut f: impl FnMut(LocalModDefId)) { + pub fn hir_for_each_module(self, mut f: impl FnMut(LocalModId)) { let crate_items = self.hir_crate_items(()); for module in crate_items.submodules.iter() { - f(LocalModDefId::new_unchecked(module.def_id)) + f(LocalModId::new_unchecked(module.def_id)) } } #[inline] - pub fn par_hir_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) { + pub fn par_hir_for_each_module(self, f: impl Fn(LocalModId) + DynSend + DynSync) { let crate_items = self.hir_crate_items(()); par_for_each_in(&crate_items.submodules[..], |module| { - f(LocalModDefId::new_unchecked(module.def_id)) + f(LocalModId::new_unchecked(module.def_id)) }) } #[inline] pub fn try_par_hir_for_each_module( self, - f: impl Fn(LocalModDefId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync, + f: impl Fn(LocalModId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync, ) -> Result<(), ErrorGuaranteed> { let crate_items = self.hir_crate_items(()); try_par_for_each_in(&crate_items.submodules[..], |module| { - f(LocalModDefId::new_unchecked(module.def_id)) + f(LocalModId::new_unchecked(module.def_id)) }) } @@ -1261,7 +1257,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> { upstream_crates } -pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems { +pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModId) -> ModuleItems { let mut collector = ItemCollector::new(tcx, false); let (hir_mod, span, hir_id) = tcx.hir_get_module(module_id); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index e0c1482af72ba..94fd6f34540da 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -12,7 +12,7 @@ use rustc_data_structures::stable_hash::{StableHash, StableHasher}; use rustc_data_structures::steal::Steal; use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in}; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap, LocalModDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap, LocalModId}; use rustc_hir::lints::DelayedLints; use rustc_hir::*; use rustc_macros::{Decodable, Encodable, StableHash}; @@ -146,22 +146,22 @@ impl ModuleItems { } impl<'tcx> TyCtxt<'tcx> { - pub fn parent_module(self, id: HirId) -> LocalModDefId { + pub fn parent_module(self, id: HirId) -> LocalModId { if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod { - LocalModDefId::new_unchecked(id.owner.def_id) + LocalModId::new_unchecked(id.owner.def_id) } else { self.parent_module_from_def_id(id.owner.def_id) } } - pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId { + pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModId { while let Some(parent) = self.opt_local_parent(id) { id = parent; if self.def_kind(id) == DefKind::Mod { break; } } - LocalModDefId::new_unchecked(id) + LocalModId::new_unchecked(id) } /// Returns `true` if this is a foreign item (i.e., linked via `extern { ... }`). diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index 05eb8727bafbd..03a0f4f4d68d0 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -64,7 +64,7 @@ use rustc_errors::{ErrorGuaranteed, catch_fatal_errors}; use rustc_hir as hir; use rustc_hir::attrs::{EiiDecl, EiiImpl, StrippedCfgItem}; use rustc_hir::def::{DefKind, DocLinkResMap}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdSet, LocalModDefId}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdSet, LocalModId}; use rustc_hir::lang_items::{LangItem, LanguageItems}; use rustc_hir::{ItemLocalId, PreciseCapturingArgKind}; use rustc_index::IndexVec; @@ -236,7 +236,7 @@ rustc_queries! { /// /// This can be conveniently accessed by `tcx.hir_visit_item_likes_in_module`. /// Avoid calling this query directly. - query hir_module_items(key: LocalModDefId) -> &'tcx rustc_middle::hir::ModuleItems { + query hir_module_items(key: LocalModId) -> &'tcx rustc_middle::hir::ModuleItems { arena_cache desc { "getting HIR module items in `{}`", tcx.def_path_str(key) } cache_on_disk @@ -1156,7 +1156,7 @@ rustc_queries! { } /// Performs lint checking for the module. - query lint_mod(key: LocalModDefId) { + query lint_mod(key: LocalModId) { desc { "linting {}", describe_as_module(key, tcx) } } @@ -1165,16 +1165,16 @@ rustc_queries! { } /// Checks the attributes in the module. - query check_mod_attrs(key: LocalModDefId) { + query check_mod_attrs(key: LocalModId) { desc { "checking attributes in {}", describe_as_module(key, tcx) } } /// Checks for uses of unstable APIs in the module. - query check_mod_unstable_api_usage(key: LocalModDefId) { + query check_mod_unstable_api_usage(key: LocalModId) { desc { "checking for unstable API usage in {}", describe_as_module(key, tcx) } } - query check_mod_privacy(key: LocalModDefId) { + query check_mod_privacy(key: LocalModId) { desc { "checking privacy in {}", describe_as_module(key.to_local_def_id(), tcx) } } @@ -1189,7 +1189,7 @@ rustc_queries! { desc { "finding live symbols in crate" } } - query check_mod_deathness(key: LocalModDefId) { + query check_mod_deathness(key: LocalModId) { desc { "checking deathness of variables in {}", describe_as_module(key, tcx) } } @@ -1380,7 +1380,7 @@ rustc_queries! { eval_always desc { "checking effective visibilities" } } - query check_private_in_public(module_def_id: LocalModDefId) { + query check_private_in_public(module_def_id: LocalModId) { desc { "checking for private elements in public interfaces for {}", describe_as_module(module_def_id, tcx) diff --git a/compiler/rustc_middle/src/query/into_query_key.rs b/compiler/rustc_middle/src/query/into_query_key.rs index 04bbfd5c3a8ab..6428a9e98e7fd 100644 --- a/compiler/rustc_middle/src/query/into_query_key.rs +++ b/compiler/rustc_middle/src/query/into_query_key.rs @@ -1,5 +1,5 @@ use rustc_hir::OwnerId; -use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModId, ModId}; /// Argument-conversion trait used by some queries and other `TyCtxt` methods. /// @@ -48,21 +48,21 @@ impl IntoQueryKey for OwnerId { } } -impl IntoQueryKey for ModDefId { +impl IntoQueryKey for ModId { #[inline(always)] fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryKey for LocalModDefId { +impl IntoQueryKey for LocalModId { #[inline(always)] fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryKey for LocalModDefId { +impl IntoQueryKey for LocalModId { #[inline(always)] fn into_query_key(self) -> LocalDefId { self.into() diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 772caa0b9f505..d542d7da618db 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -7,7 +7,7 @@ use std::hash::Hash; use rustc_ast::tokenstream::TokenStream; use rustc_data_structures::sso::SsoHashSet; use rustc_data_structures::stable_hash::StableHash; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModId}; use rustc_hir::hir_id::OwnerId; use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol}; @@ -157,7 +157,7 @@ impl QueryKey for DefId { } } -impl QueryKey for LocalModDefId { +impl QueryKey for LocalModId { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(*self) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 7d7ca37ff3243..a6bf60e79be06 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -11,7 +11,7 @@ use rustc_data_structures::unord::UnordMap; use rustc_hir as hir; use rustc_hir::LangItem; use rustc_hir::def::{self, CtorKind, DefKind, Namespace}; -use rustc_hir::def_id::{DefIdMap, DefIdSet, LOCAL_CRATE, ModDefId}; +use rustc_hir::def_id::{DefIdMap, DefIdSet, LOCAL_CRATE, ModId}; use rustc_hir::definitions::{DefKey, DefPathDataName}; use rustc_hir::limit::Limit; use rustc_macros::{Lift, extension}; @@ -385,8 +385,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { && Some(*visible_parent) != actual_parent { this.tcx() - // FIXME(typed_def_id): Further propagate ModDefId - .module_children(ModDefId::new_unchecked(*visible_parent)) + // FIXME(typed_def_id): Further propagate ModId + .module_children(ModId::new_unchecked(*visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) @@ -612,8 +612,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { // that's public and whose identifier isn't `_`. let reexport = self .tcx() - // FIXME(typed_def_id): Further propagate ModDefId - .module_children(ModDefId::new_unchecked(visible_parent)) + // FIXME(typed_def_id): Further propagate ModId + .module_children(ModId::new_unchecked(visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index fee9cd80161e5..29e9d1243f0ca 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -140,7 +140,7 @@ impl ImplRestrictionKind { if restricted_to.krate == rustc_hir::def_id::LOCAL_CRATE { with_crate_prefix!(with_no_trimmed_paths!(tcx.def_path_str(restricted_to))) } else { - tcx.def_path_str(restricted_to.krate.as_mod_def_id()) + tcx.def_path_str(restricted_to.krate.as_mod_id()) } } } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index bf2dd237cd797..dd8272cc6ca25 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -21,7 +21,7 @@ use rustc_hir::attrs::{ OptimizeAttr, ReprAttr, }; use rustc_hir::def::DefKind; -use rustc_hir::def_id::LocalModDefId; +use rustc_hir::def_id::LocalModId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{ self as hir, Attribute, CRATE_HIR_ID, Constness, FnSig, ForeignItem, GenericParam, @@ -1783,7 +1783,7 @@ fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>) } } -fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { +fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalModId) { let check_attr_visitor = &mut CheckAttrVisitor { tcx, abort: Cell::new(false) }; tcx.hir_visit_item_likes_in_module(module_def_id, check_attr_visitor); if module_def_id.to_local_def_id().is_top_level_module() { diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 221aca2ec0c95..dd5247823231d 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -12,7 +12,7 @@ use rustc_abi::FieldIdx; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_errors::{ErrorGuaranteed, MultiSpan}; use rustc_hir::def::{CtorOf, DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModId}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{self as hir, ForeignItemId, ItemId, Node, PatKind, QPath, find_attr}; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; @@ -1325,7 +1325,7 @@ impl<'tcx> DeadVisitor<'tcx> { } } -fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) { +fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModId) { let Ok(DeadCodeLivenessSummary { pre_deferred_seeding, final_result }) = tcx.live_symbols_and_ignored_derived_traits(()).as_ref() else { @@ -1367,7 +1367,7 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) { fn lint_dead_codes<'tcx>( tcx: TyCtxt<'tcx>, target_lint: &'static Lint, - module: LocalModDefId, + module: LocalModId, live_symbols: &'tcx LocalDefIdSet, ignored_derived_traits: &'tcx LocalDefIdMap>, free_items: impl Iterator, diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index bf5a1dc2ec4b8..86e3e57b34bf0 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -9,7 +9,7 @@ use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet}; use rustc_feature::{EnabledLangFeature, EnabledLibFeature, UNSTABLE_LANG_FEATURES}; use rustc_hir::attrs::{AttributeKind, DeprecatedSince}; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModId}; use rustc_hir::intravisit::{self, Visitor, VisitorExt}; use rustc_hir::{ self as hir, AmbigArg, ConstStability, Constness, DefaultBodyStability, FieldDef, HirId, Item, @@ -520,21 +520,21 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { /// Cross-references the feature names of unstable APIs with enabled /// features and possibly prints errors. -fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { - tcx.hir_visit_item_likes_in_module(module_def_id, &mut Checker { tcx }); +fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, mod_id: LocalModId) { + tcx.hir_visit_item_likes_in_module(mod_id, &mut Checker { tcx }); let is_staged_api = tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api(); if is_staged_api { let effective_visibilities = &tcx.effective_visibilities(()); let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities }; - if module_def_id.is_top_level_module() { + if mod_id.is_top_level_module() { missing.check_missing_stability(CRATE_DEF_ID); } - tcx.hir_visit_item_likes_in_module(module_def_id, &mut missing); + tcx.hir_visit_item_likes_in_module(mod_id, &mut missing); } - if module_def_id.is_top_level_module() { + if mod_id.is_top_level_module() { check_unused_or_stable_features(tcx) } } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 46d6445711175..0e7dfe18d1048 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -21,7 +21,7 @@ use rustc_data_structures::indexmap::IndexSet; use rustc_data_structures::intern::Interned; use rustc_errors::{MultiSpan, listify}; use rustc_hir::def::{CtorOf, DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModId}; use rustc_hir::intravisit::{self, InferKind, Visitor}; use rustc_hir::{self as hir, AmbigArg, ForeignItemId, ItemId, OwnerId, PatKind, find_attr}; use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level}; @@ -517,7 +517,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { max_vis: Option, level: Level, ) -> bool { - // FIXME(typed_def_id): Make `Visibility::Restricted` use a `LocalModDefId` by default. + // FIXME(typed_def_id): Make `Visibility::Restricted` use a `LocalModId` by default. let private_vis = ty::Visibility::Restricted(self.tcx.parent_module_from_def_id(def_id).into()); if max_vis != Some(private_vis) { @@ -1128,14 +1128,14 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> { /// Checks are performed on "semantic" types regardless of names and their hygiene. struct TypePrivacyVisitor<'tcx> { tcx: TyCtxt<'tcx>, - module_def_id: LocalModDefId, + mod_id: LocalModId, maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>, span: Span, } impl<'tcx> TypePrivacyVisitor<'tcx> { fn item_is_accessible(&self, did: DefId) -> bool { - self.tcx.visibility(did).is_accessible_from(self.module_def_id, self.tcx) + self.tcx.visibility(did).is_accessible_from(self.mod_id, self.tcx) } // Take node-id of an expression or pattern and check its type for privacy. @@ -1746,17 +1746,17 @@ pub fn provide(providers: &mut Providers) { }; } -fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { +fn check_mod_privacy(tcx: TyCtxt<'_>, mod_id: LocalModId) { // Check privacy of names not checked in previous compilation stages. let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None }; - tcx.hir_visit_item_likes_in_module(module_def_id, &mut visitor); + tcx.hir_visit_item_likes_in_module(mod_id, &mut visitor); // Check privacy of explicitly written types and traits as well as // inferred types of expressions and patterns. - let span = tcx.def_span(module_def_id); - let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results: None, span }; + let span = tcx.def_span(mod_id); + let mut visitor = TypePrivacyVisitor { tcx, mod_id, maybe_typeck_results: None, span }; - let module = tcx.hir_module_items(module_def_id); + let module = tcx.hir_module_items(mod_id); for def_id in module.definitions() { let _ = rustc_ty_utils::sig_types::walk_types(tcx, def_id, &mut visitor); @@ -1881,12 +1881,12 @@ fn effective_visibilities(tcx: TyCtxt<'_>, (): ()) -> &EffectiveVisibilities { tcx.arena.alloc(visitor.effective_visibilities) } -fn check_private_in_public(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) { +fn check_private_in_public(tcx: TyCtxt<'_>, mod_id: LocalModId) { let effective_visibilities = tcx.effective_visibilities(()); // Check for private types in public interfaces. let checker = PrivateItemsInPublicInterfacesChecker { tcx, effective_visibilities }; - let crate_items = tcx.hir_module_items(module_def_id); + let crate_items = tcx.hir_module_items(mod_id); let _ = crate_items.par_items(|id| Ok(checker.check_item(id))); let _ = crate_items.par_foreign_items(|id| Ok(checker.check_foreign_item(id))); } diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 0f85610ab665a..e9de1175561f7 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -40,8 +40,8 @@ impl CrateNum { } #[inline] - pub fn as_mod_def_id(self) -> ModDefId { - ModDefId::new_unchecked(DefId { krate: self, index: CRATE_DEF_INDEX }) + pub fn as_mod_id(self) -> ModId { + ModId::new_unchecked(DefId { krate: self, index: CRATE_DEF_INDEX }) } } @@ -377,6 +377,7 @@ impl !Ord for LocalDefId {} impl !PartialOrd for LocalDefId {} pub const CRATE_DEF_ID: LocalDefId = LocalDefId { local_def_index: CRATE_DEF_INDEX }; +pub const CRATE_MOD_ID: LocalModId = LocalModId::new_unchecked(CRATE_DEF_ID); impl Idx for LocalDefId { #[inline] @@ -466,9 +467,9 @@ impl ToStableHashKey for LocalDefId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] -pub struct ModDefId(DefId); +pub struct ModId(DefId); -impl ModDefId { +impl ModId { #[inline] pub const fn new_unchecked(def_id: DefId) -> Self { Self(def_id) @@ -485,8 +486,8 @@ impl ModDefId { } #[inline] - pub fn as_local(self) -> Option { - self.0.as_local().map(LocalModDefId::new_unchecked) + pub fn as_local(self) -> Option { + self.0.as_local().map(LocalModId::new_unchecked) } pub fn is_top_level_module(self) -> bool { @@ -494,29 +495,27 @@ impl ModDefId { } } -impl From for ModDefId { +impl From for ModId { #[inline] - fn from(local: LocalModDefId) -> Self { + fn from(local: LocalModId) -> Self { Self(local.0.to_def_id()) } } -impl From for DefId { +impl From for DefId { #[inline] - fn from(typed: ModDefId) -> Self { + fn from(typed: ModId) -> Self { typed.0 } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, StableHash)] -pub struct LocalModDefId(LocalDefId); +pub struct LocalModId(LocalDefId); -impl !Ord for LocalModDefId {} -impl !PartialOrd for LocalModDefId {} - -impl LocalModDefId { - pub const CRATE_DEF_ID: Self = Self::new_unchecked(CRATE_DEF_ID); +impl !Ord for LocalModId {} +impl !PartialOrd for LocalModId {} +impl LocalModId { #[inline] pub const fn new_unchecked(def_id: LocalDefId) -> Self { Self(def_id) @@ -537,16 +536,16 @@ impl LocalModDefId { } } -impl From for LocalDefId { +impl From for LocalDefId { #[inline] - fn from(typed: LocalModDefId) -> Self { + fn from(typed: LocalModId) -> Self { typed.0 } } -impl From for DefId { +impl From for DefId { #[inline] - fn from(typed: LocalModDefId) -> Self { + fn from(typed: LocalModId) -> Self { typed.0.into() } } From 3c0923a48d05ae0740b18d8b556c469a2fef5eb9 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Sat, 11 Jul 2026 20:59:20 -0500 Subject: [PATCH 4/6] Use ModId more --- .../src/debuginfo/metadata.rs | 2 +- compiler/rustc_expand/src/base.rs | 4 +-- .../src/check/compare_impl_item/refine.rs | 3 +- .../src/hir_ty_lowering/mod.rs | 7 ++-- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_lint/src/unused/must_use.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 5 +-- .../src/rmeta/decoder/cstore_impl.rs | 8 +++++ compiler/rustc_metadata/src/rmeta/encoder.rs | 15 ++++++--- compiler/rustc_middle/src/hir/map.rs | 18 ++++------ compiler/rustc_middle/src/hir/mod.rs | 2 +- compiler/rustc_middle/src/metadata.rs | 4 +-- compiler/rustc_middle/src/queries.rs | 8 ++--- compiler/rustc_middle/src/query/erase.rs | 3 +- compiler/rustc_middle/src/query/keys.rs | 19 +++++++++++ compiler/rustc_middle/src/ty/assoc.rs | 3 +- compiler/rustc_middle/src/ty/context.rs | 6 ++-- .../ty/inhabitedness/inhabited_predicate.rs | 11 ++++--- .../rustc_middle/src/ty/inhabitedness/mod.rs | 3 +- compiler/rustc_middle/src/ty/mod.rs | 27 ++++++++------- compiler/rustc_middle/src/ty/print/pretty.rs | 2 -- .../rustc_middle/src/ty/structural_impls.rs | 1 + compiler/rustc_mir_build/src/builder/scope.rs | 2 +- .../src/thir/pattern/check_match.rs | 2 +- .../src/lint_and_remove_uninhabited.rs | 2 +- compiler/rustc_passes/src/check_export.rs | 2 +- compiler/rustc_pattern_analysis/src/rustc.rs | 4 +-- compiler/rustc_privacy/src/lib.rs | 14 +++----- .../rustc_resolve/src/build_reduced_graph.rs | 22 +++++++------ .../rustc_resolve/src/diagnostics/impls.rs | 7 ++-- .../src/effective_visibilities.rs | 17 ++++++---- compiler/rustc_resolve/src/ident.rs | 4 ++- compiler/rustc_resolve/src/imports.rs | 4 +-- compiler/rustc_resolve/src/lib.rs | 33 ++++++++++--------- compiler/rustc_resolve/src/macros.rs | 10 +++--- compiler/rustc_span/src/def_id.rs | 8 +++++ compiler/rustc_span/src/hygiene.rs | 10 +++--- .../thir-tree-field-expr-index.stdout | 16 ++++----- tests/ui/thir-print/thir-tree-match.stdout | 6 ++-- 39 files changed, 185 insertions(+), 133 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 26d98ec13cc2b..e3b8af5e8c4db 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1044,7 +1044,7 @@ fn visibility_di_flags<'ll, 'tcx>( match visibility { Visibility::Public => DIFlags::FlagPublic, // Private fields have a restricted visibility of the module containing the type. - Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate, + Visibility::Restricted(did) if did.to_def_id() == parent_did => DIFlags::FlagPrivate, // `pub(crate)`/`pub(super)` visibilities are any other restricted visibility. Visibility::Restricted(..) => DIFlags::FlagProtected, } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 24501524edf04..9831b203ad732 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -24,7 +24,7 @@ use rustc_parse::MACRO_ARGUMENTS; use rustc_parse::parser::Parser; use rustc_session::Session; use rustc_session::parse::ParseSess; -use rustc_span::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_span::def_id::{CrateNum, DefId, LocalDefId, ModId}; use rustc_span::edition::Edition; use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind}; use rustc_span::source_map::SourceMap; @@ -1003,7 +1003,7 @@ impl SyntaxExtension { descr: Symbol, kind: MacroKind, macro_def_id: Option, - parent_module: Option, + parent_module: Option, ) -> ExpnData { ExpnData::new( ExpnKind::Macro(kind, descr), diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs index 074196b0f959e..70ab2cc44e0c6 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs @@ -12,6 +12,7 @@ use rustc_middle::ty::{ TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized, }; use rustc_span::Span; +use rustc_span::def_id::ModId; use rustc_trait_selection::regions::InferCtxtRegionExt; use rustc_trait_selection::traits::{ObligationCtxt, elaborate, normalize_param_env_or_error}; @@ -380,7 +381,7 @@ fn report_mismatched_rpitit_signature<'tcx>( ); } -fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { +fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { match *ty.kind() { ty::Ref(_, ty, _) => type_visibility(tcx, ty), ty::Adt(def, args) => { diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 5778a0f932374..29c0786e66f93 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -45,6 +45,7 @@ use rustc_middle::ty::{ use rustc_middle::{bug, span_bug}; use rustc_session::diagnostics::feature_err; use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS; +use rustc_span::def_id::ModId; use rustc_span::{DUMMY_SP, Ident, Span, kw, sym}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::{self, FulfillmentError}; @@ -116,7 +117,7 @@ pub enum RegionInferReason<'a> { pub struct InherentAssocCandidate { pub impl_: DefId, pub assoc_item: DefId, - pub scope: DefId, + pub scope: ModId, } pub struct ResolvedStructPath<'tcx> { @@ -1806,7 +1807,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ident: Ident, assoc_tag: ty::AssocTag, scope: DefId, - ) -> Option<(ty::AssocItem, /*scope*/ DefId)> { + ) -> Option<(ty::AssocItem, /*scope*/ ModId)> { let tcx = self.tcx(); let (ident, def_scope) = tcx.adjust_ident_and_get_scope(ident, scope, self.item_def_id()); @@ -1826,7 +1827,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { &self, item_def_id: DefId, ident: Ident, - scope: DefId, + scope: ModId, block: HirId, span: Span, ) { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ea0f8f22ab187..c34005d2ea7f5 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1192,7 +1192,7 @@ impl UnreachablePub { && let parent_parent = cx .tcx .parent_module_from_def_id(cx.tcx.parent_module_from_def_id(def_id).into()) - && *restricted_did == parent_parent.to_local_def_id() + && *restricted_did == parent_parent && !restricted_did.to_def_id().is_crate_root() { "pub(super)" diff --git a/compiler/rustc_lint/src/unused/must_use.rs b/compiler/rustc_lint/src/unused/must_use.rs index c7177f1829bbd..e72335c80d505 100644 --- a/compiler/rustc_lint/src/unused/must_use.rs +++ b/compiler/rustc_lint/src/unused/must_use.rs @@ -143,7 +143,7 @@ pub fn is_ty_must_use<'tcx>( return IsTyMustUse::Trivial; } - let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id(); + let parent_mod_did = cx.tcx.parent_module(expr.hir_id); let is_uninhabited = |t: Ty<'tcx>| !t.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env()); diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index bed2be51a3468..5719d6610b6bc 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -32,6 +32,7 @@ use rustc_serialize::{Decodable, Decoder}; use rustc_session::config::TargetModifier; use rustc_session::config::mitigation_coverage::DeniedPartialMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; +use rustc_span::def_id::ModId; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ BlobDecoder, BytePos, ByteSymbol, DUMMY_SP, Pos, RemapPathScopeComponents, SpanData, @@ -1173,14 +1174,14 @@ impl CrateMetadata { ) } - fn get_visibility(&self, tcx: TyCtxt<'_>, id: DefIndex) -> Visibility { + fn get_visibility(&self, tcx: TyCtxt<'_>, id: DefIndex) -> Visibility { self.root .tables .visibility .get(self, id) .unwrap_or_else(|| self.missing("visibility", id)) .decode((self, tcx)) - .map_id(|index| self.local_def_id(index)) + .map_id(|index| ModId::new_unchecked(self.local_def_id(index))) } fn get_safety(&self, id: DefIndex) -> Safety { diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 798709d69d76e..69f7540c08e83 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -19,6 +19,7 @@ use rustc_middle::util::Providers; use rustc_serialize::Decoder; use rustc_session::StableCrateId; use rustc_session::cstore::{CrateStore, ExternCrate}; +use rustc_span::def_id::ModId; use rustc_span::hygiene::ExpnId; use rustc_span::{Span, Symbol, kw}; @@ -191,6 +192,13 @@ impl IntoArgs for DefId { } } +impl IntoArgs for ModId { + type Other = (); + fn into_args(self) -> (DefId, ()) { + (self.to_def_id(), ()) + } +} + impl IntoArgs for CrateNum { type Other = (); fn into_args(self) -> (DefId, ()) { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index cd5e343e08d1a..90ee009facacd 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -28,6 +28,7 @@ use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; use rustc_session::config::mitigation_coverage::DeniedPartialMitigation; use rustc_session::config::{CrateType, OptLevel, TargetModifier}; +use rustc_span::def_id::CRATE_MOD_ID; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId, @@ -1455,8 +1456,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); } if should_encode_visibility(def_kind) { - let vis = - self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index); + let vis = self + .tcx + .local_visibility(local_id) + .map_id(|mod_id| mod_id.to_local_def_id().local_def_index); record!(self.tables.visibility[def_id] <- vis); } if should_encode_stability(def_kind) { @@ -1979,16 +1982,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod); record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local()); - let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index); + let vis = tcx + .local_visibility(CRATE_DEF_ID) + .map_id(|mod_id| mod_id.to_local_def_id().local_def_index); record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); if let Some(stability) = stability { record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); } self.encode_deprecation(LOCAL_CRATE.as_def_id()); - if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_DEF_ID) { + if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_MOD_ID) { record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map); } - if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_DEF_ID) { + if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_MOD_ID) { record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits); } diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index b0abc01af9d17..c01d9e98e9b9c 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -499,17 +499,15 @@ impl<'tcx> TyCtxt<'tcx> { pub fn hir_for_each_module(self, mut f: impl FnMut(LocalModId)) { let crate_items = self.hir_crate_items(()); - for module in crate_items.submodules.iter() { - f(LocalModId::new_unchecked(module.def_id)) + for &module in crate_items.submodules.iter() { + f(module) } } #[inline] pub fn par_hir_for_each_module(self, f: impl Fn(LocalModId) + DynSend + DynSync) { let crate_items = self.hir_crate_items(()); - par_for_each_in(&crate_items.submodules[..], |module| { - f(LocalModId::new_unchecked(module.def_id)) - }) + par_for_each_in(&crate_items.submodules[..], |&&module| f(module)); } #[inline] @@ -518,9 +516,7 @@ impl<'tcx> TyCtxt<'tcx> { f: impl Fn(LocalModId) -> Result<(), ErrorGuaranteed> + DynSend + DynSync, ) -> Result<(), ErrorGuaranteed> { let crate_items = self.hir_crate_items(()); - try_par_for_each_in(&crate_items.submodules[..], |module| { - f(LocalModId::new_unchecked(module.def_id)) - }) + try_par_for_each_in(&crate_items.submodules[..], |&&module| f(module)) } /// Returns an iterator for the nodes in the ancestor tree of the `current_id` @@ -1298,7 +1294,7 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems { // A "crate collector" and "module collector" start at a // module item (the former starts at the crate root) but only // the former needs to collect it. ItemCollector does not do this for us. - collector.submodules.push(CRATE_OWNER_ID); + collector.submodules.push(CRATE_MOD_ID); tcx.hir_walk_toplevel_module(&mut collector); let ItemCollector { @@ -1337,7 +1333,7 @@ struct ItemCollector<'tcx> { // (see ). crate_collector: bool, tcx: TyCtxt<'tcx>, - submodules: Vec = vec![], + submodules: Vec = vec![], items: Vec = vec![], trait_items: Vec = vec![], impl_items: Vec = vec![], @@ -1386,7 +1382,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> { // Items that are modules are handled here instead of in visit_mod. if let ItemKind::Mod(_, module) = &item.kind { - self.submodules.push(item.owner_id); + self.submodules.push(LocalModId::new_unchecked(item.owner_id.def_id)); // A module collector does not recurse inside nested modules. if self.crate_collector { intravisit::walk_mod(self, module); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 94fd6f34540da..13bda2991fa92 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -28,7 +28,7 @@ pub struct ModuleItems { /// Whether this represents the whole crate, in which case we need to add `CRATE_OWNER_ID` to /// the iterators if we want to account for the crate root. add_root: bool, - submodules: Box<[OwnerId]>, + submodules: Box<[LocalModId]>, free_items: Box<[ItemId]>, trait_items: Box<[TraitItemId]>, impl_items: Box<[ImplItemId]>, diff --git a/compiler/rustc_middle/src/metadata.rs b/compiler/rustc_middle/src/metadata.rs index 1365d2e19b75f..0c9b44a93a20e 100644 --- a/compiler/rustc_middle/src/metadata.rs +++ b/compiler/rustc_middle/src/metadata.rs @@ -1,7 +1,7 @@ use rustc_hir::def::Res; use rustc_macros::{StableHash, TyDecodable, TyEncodable}; use rustc_span::Ident; -use rustc_span::def_id::DefId; +use rustc_span::def_id::{DefId, ModId}; use smallvec::SmallVec; use crate::ty; @@ -39,7 +39,7 @@ pub struct ModChild { /// Local variables cannot be exported, so this `Res` doesn't need the ID parameter. pub res: Res, /// Visibility of the item. - pub vis: ty::Visibility, + pub vis: ty::Visibility, /// Reexport chain linking this module child to its original reexported item. /// Empty if the module child is a proper item. pub reexport_chain: SmallVec<[Reexport; 2]>, diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index 03a0f4f4d68d0..86eca36eac084 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -76,7 +76,7 @@ use rustc_session::cstore::{ CrateDepKind, CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, }; use rustc_session::lint::StableLintExpectationId; -use rustc_span::def_id::LOCAL_CRATE; +use rustc_span::def_id::{LOCAL_CRATE, ModId}; use rustc_span::{DUMMY_SP, LocalExpnId, Span, Spanned, Symbol}; use rustc_target::spec::PanicStrategy; @@ -2164,7 +2164,7 @@ rustc_queries! { /// ``` /// /// In here, if you call `visibility` on `T`, it'll panic. - query visibility(def_id: DefId) -> ty::Visibility { + query visibility(def_id: DefId) -> ty::Visibility { desc { "computing visibility of `{}`", tcx.def_path_str(def_id) } separate_provide_extern feedable @@ -2692,13 +2692,13 @@ rustc_queries! { separate_provide_extern } - query doc_link_resolutions(def_id: DefId) -> &'tcx DocLinkResMap { + query doc_link_resolutions(def_id: ModId) -> &'tcx DocLinkResMap { eval_always desc { "resolutions for documentation links for a module" } separate_provide_extern } - query doc_link_traits_in_scope(def_id: DefId) -> &'tcx [DefId] { + query doc_link_traits_in_scope(def_id: ModId) -> &'tcx [DefId] { eval_always desc { "traits in scope for documentation links for a module" } separate_provide_extern diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index a5fb77ced7656..827ae5a8edbdf 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -13,6 +13,7 @@ use std::mem::MaybeUninit; use rustc_ast::tokenstream::TokenStream; use rustc_data_structures::steal::Steal; use rustc_data_structures::sync::{DynSend, DynSync}; +use rustc_span::def_id::ModId; use rustc_span::{ErrorGuaranteed, Spanned}; use crate::mono::{MonoItem, NormalizationErrorInMono}; @@ -246,7 +247,7 @@ impl_erasable_for_types_with_no_type_params! { rustc_middle::ty::ParamEnv<'_>, rustc_middle::ty::SymbolName<'_>, rustc_middle::ty::TypingEnv<'_>, - rustc_middle::ty::Visibility, + rustc_middle::ty::Visibility, rustc_middle::ty::inhabitedness::InhabitedPredicate<'_>, rustc_session::Limits, rustc_session::config::OptLevel, diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index d542d7da618db..225618689647d 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -9,6 +9,7 @@ use rustc_data_structures::sso::SsoHashSet; use rustc_data_structures::stable_hash::StableHash; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModId}; use rustc_hir::hir_id::OwnerId; +use rustc_span::def_id::ModId; use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol}; use crate::dep_graph::DepNodeIndex; @@ -157,6 +158,24 @@ impl QueryKey for DefId { } } +impl QueryKey for ModId { + type LocalQueryKey = LocalModId; + + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { + tcx.def_span(self.to_def_id()) + } + + #[inline(always)] + fn key_as_def_id(&self) -> Option { + Some(self.to_def_id()) + } + + #[inline(always)] + fn as_local_key(&self) -> Option { + self.as_local() + } +} + impl QueryKey for LocalModId { fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(*self) diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index 2c59e66c6f470..85c94d0b598e6 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -3,6 +3,7 @@ use rustc_hir as hir; use rustc_hir::def::{DefKind, Namespace}; use rustc_hir::def_id::DefId; use rustc_macros::{Decodable, Encodable, StableHash}; +use rustc_span::def_id::ModId; use rustc_span::{ErrorGuaranteed, Ident, Symbol}; use super::{TyCtxt, Visibility}; @@ -82,7 +83,7 @@ impl AssocItem { } #[inline] - pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility { + pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility { tcx.visibility(self.def_id) } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 17f0098070c5c..146662676acc0 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -620,7 +620,7 @@ impl<'tcx> TyCtxt<'tcx> { other => bug!("{key:?} is not an assoc item of a trait impl: {other:?}"), } } - TyCtxtFeed { tcx: self, key }.visibility(vis.to_def_id()) + TyCtxtFeed { tcx: self, key }.visibility(vis.to_mod_id()) } } @@ -1324,8 +1324,8 @@ impl<'tcx> TyCtxt<'tcx> { // Visibilities for opaque types are meaningless, but still provided // so that all items have visibilities. if matches!(def_kind, DefKind::Closure | DefKind::OpaqueTy) { - let parent_mod = self.parent_module_from_def_id(def_id).to_def_id(); - feed.visibility(ty::Visibility::Restricted(parent_mod)); + let parent_mod = self.parent_module_from_def_id(def_id); + feed.visibility(ty::Visibility::Restricted(parent_mod.to_mod_id())); } feed diff --git a/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs b/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs index 03a6163b33ade..6cd188349ef22 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs @@ -1,8 +1,9 @@ use rustc_macros::StableHash; +use rustc_span::def_id::{LocalModId, ModId}; use smallvec::SmallVec; use tracing::instrument; -use crate::ty::{self, DefId, OpaqueTypeKey, Ty, TyCtxt, TypingEnv, Unnormalized}; +use crate::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypingEnv, Unnormalized}; /// Represents whether some type is inhabited in a given context. /// Examples of uninhabited types are `!`, `enum Void {}`, or a struct @@ -20,7 +21,7 @@ pub enum InhabitedPredicate<'tcx> { ConstIsZero(ty::Const<'tcx>), /// Uninhabited if within a certain module. This occurs when an uninhabited /// type has restricted visibility. - NotInModule(DefId), + NotInModule(ModId), /// Inhabited if some generic type is inhabited. /// These are replaced by calling [`Self::instantiate`]. GenericType(Ty<'tcx>), @@ -38,7 +39,7 @@ impl<'tcx> InhabitedPredicate<'tcx> { self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>, - module_def_id: DefId, + module_def_id: LocalModId, ) -> bool { self.apply_revealing_opaque(tcx, typing_env, module_def_id, &|_| None) } @@ -49,7 +50,7 @@ impl<'tcx> InhabitedPredicate<'tcx> { self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>, - module_def_id: DefId, + module_def_id: LocalModId, reveal_opaque: &impl Fn(OpaqueTypeKey<'tcx>) -> Option>, ) -> bool { let Ok(result) = self.apply_inner::( @@ -83,7 +84,7 @@ impl<'tcx> InhabitedPredicate<'tcx> { tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>, eval_stack: &mut SmallVec<[Ty<'tcx>; 1]>, // for cycle detection - in_module: &impl Fn(DefId) -> Result, + in_module: &impl Fn(ModId) -> Result, reveal_opaque: &impl Fn(OpaqueTypeKey<'tcx>) -> Option>, ) -> Result { match self { diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index 55b359866bb97..12c13ad74cb24 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -43,6 +43,7 @@ //! This code should only compile in modules where the uninhabitedness of `Foo` //! is visible. +use rustc_span::def_id::LocalModId; use rustc_type_ir::TyKind::*; use tracing::instrument; @@ -184,7 +185,7 @@ impl<'tcx> Ty<'tcx> { pub fn is_inhabited_from( self, tcx: TyCtxt<'tcx>, - module: DefId, + module: LocalModId, typing_env: ty::TypingEnv<'tcx>, ) -> bool { self.inhabited_predicate(tcx).apply(tcx, typing_env, module) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 6916083071d5a..6412065a3088d 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -50,6 +50,7 @@ use rustc_macros::{ use rustc_serialize::{Decodable, Encodable}; use rustc_session::config::OptLevel; pub use rustc_session::lint::RegisteredTools; +use rustc_span::def_id::{LocalModId, ModId}; use rustc_span::hygiene::MacroKind; use rustc_span::{DUMMY_SP, ExpnId, ExpnKind, Ident, Span, Symbol}; use rustc_target::callconv::FnAbi; @@ -199,8 +200,8 @@ pub struct ResolverGlobalCtxt { /// Mapping from ident span to path span for paths that don't exist as written, but that /// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`. pub confused_type_with_std_module: FxIndexMap, - pub doc_link_resolutions: FxIndexMap, - pub doc_link_traits_in_scope: FxIndexMap>, + pub doc_link_resolutions: FxIndexMap, + pub doc_link_traits_in_scope: FxIndexMap>, pub all_macro_rules: UnordSet, pub stripped_cfg_items: Vec, // Information about delegations which is used when handling recursive delegations @@ -311,7 +312,7 @@ impl Asyncness { } #[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Encodable, BlobDecodable, StableHash)] -pub enum Visibility { +pub enum Visibility { /// Visible everywhere (including in other crates). Public, /// Visible only in the given crate-local module. @@ -324,7 +325,7 @@ impl Visibility { ty::Visibility::Restricted(restricted_id) => { if restricted_id.is_top_level_module() { "pub(crate)".to_string() - } else if restricted_id == tcx.parent_module_from_def_id(def_id).to_local_def_id() { + } else if restricted_id == tcx.parent_module_from_def_id(def_id) { "pub(self)".to_string() } else { format!( @@ -428,11 +429,13 @@ impl Visibility { } } -impl> Visibility { - pub fn to_def_id(self) -> Visibility { - self.map_id(Into::into) +impl Visibility { + pub fn to_mod_id(self) -> Visibility { + self.map_id(LocalModId::to_mod_id) } +} +impl> Visibility { /// Returns `true` if an item with this visibility is accessible from the given module. pub fn is_accessible_from(self, module: impl Into, tcx: TyCtxt<'_>) -> bool { match self { @@ -477,7 +480,7 @@ impl + Debug + Copy> Visibility { } } -impl Visibility { +impl Visibility { pub fn expect_local(self) -> Visibility { self.map_id(|id| id.expect_local()) } @@ -486,7 +489,7 @@ impl Visibility { pub fn is_visible_locally(self) -> bool { match self { Visibility::Public => true, - Visibility::Restricted(def_id) => def_id.is_local(), + Visibility::Restricted(mod_id) => mod_id.is_local(), } } } @@ -1468,7 +1471,7 @@ pub enum VariantDiscr { pub struct FieldDef { pub did: DefId, pub name: Symbol, - pub vis: Visibility, + pub vis: Visibility, pub safety: hir::Safety, pub value: Option, } @@ -2162,12 +2165,12 @@ impl<'tcx> TyCtxt<'tcx> { mut ident: Ident, scope: DefId, item_id: LocalDefId, - ) -> (Ident, DefId) { + ) -> (Ident, ModId) { let scope = ident .span .normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope)) .and_then(|actual_expansion| actual_expansion.expn_data().parent_module) - .unwrap_or_else(|| self.parent_module_from_def_id(item_id).to_def_id()); + .unwrap_or_else(|| self.parent_module_from_def_id(item_id).to_mod_id()); (ident, scope) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index a6bf60e79be06..d0fd3a88819be 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -385,7 +385,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { && Some(*visible_parent) != actual_parent { this.tcx() - // FIXME(typed_def_id): Further propagate ModId .module_children(ModId::new_unchecked(*visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) @@ -612,7 +611,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { // that's public and whose identifier isn't `_`. let reexport = self .tcx() - // FIXME(typed_def_id): Further propagate ModId .module_children(ModId::new_unchecked(visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 49bac6a130231..7e087f6e1646f 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -250,6 +250,7 @@ TrivialTypeTraversalImpls! { rustc_span::Ident, rustc_span::Span, rustc_span::Symbol, + rustc_span::def_id::ModId, rustc_target::asm::InlineAsmRegOrRegClass, // tidy-alphabetical-end } diff --git a/compiler/rustc_mir_build/src/builder/scope.rs b/compiler/rustc_mir_build/src/builder/scope.rs index 932e4af130459..a5707e0177c63 100644 --- a/compiler/rustc_mir_build/src/builder/scope.rs +++ b/compiler/rustc_mir_build/src/builder/scope.rs @@ -967,7 +967,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let cx = RustcPatCtxt { tcx: self.tcx, typeck_results, - module: self.tcx.parent_module(self.hir_id).to_def_id(), + module: self.tcx.parent_module(self.hir_id), typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build( self.tcx, self.def_id, diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 57a71467f6f01..41806547932cd 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -385,7 +385,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { tcx: self.tcx, typeck_results: self.typeck_results, typing_env: self.typing_env, - module: self.tcx.parent_module(self.hir_source).to_def_id(), + module: self.tcx.parent_module(self.hir_source), dropless_arena: self.dropless_arena, match_lint_level: self.hir_source, whole_match_span, diff --git a/compiler/rustc_mir_transform/src/lint_and_remove_uninhabited.rs b/compiler/rustc_mir_transform/src/lint_and_remove_uninhabited.rs index b359077753fd6..7d902c0149c95 100644 --- a/compiler/rustc_mir_transform/src/lint_and_remove_uninhabited.rs +++ b/compiler/rustc_mir_transform/src/lint_and_remove_uninhabited.rs @@ -14,7 +14,7 @@ impl<'tcx> crate::MirPass<'tcx> for LintAndRemoveUninhabited { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let def_id = body.source.def_id().expect_local(); tracing::debug!(?def_id); - let parent_module = tcx.parent_module_from_def_id(def_id).to_def_id(); + let parent_module = tcx.parent_module_from_def_id(def_id); let typing_env = body.typing_env(tcx); // check if the function's return type is inhabited diff --git a/compiler/rustc_passes/src/check_export.rs b/compiler/rustc_passes/src/check_export.rs index a1b204152387c..411aad0717959 100644 --- a/compiler/rustc_passes/src/check_export.rs +++ b/compiler/rustc_passes/src/check_export.rs @@ -56,7 +56,7 @@ impl<'tcx> ExportableItemCollector<'tcx> { if has_attr && !is_pub { let vis = visibilities.effective_vis(def_id).cloned().unwrap_or_else(|| { EffectiveVisibility::from_vis(Visibility::Restricted( - self.tcx.parent_module_from_def_id(def_id).to_local_def_id(), + self.tcx.parent_module_from_def_id(def_id), )) }); let vis = vis.at_level(Level::Direct); diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index 14fd5f8e9a2dc..c3b3d14848982 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -5,7 +5,6 @@ use std::iter::once; use rustc_abi::{FIRST_VARIANT, FieldIdx, Integer, VariantIdx}; use rustc_arena::DroplessArena; use rustc_hir::HirId; -use rustc_hir::def_id::DefId; use rustc_index::{Idx, IndexVec}; use rustc_middle::middle::stability::EvalResult; use rustc_middle::thir::{self, Pat, PatKind, PatRange, PatRangeBoundary}; @@ -15,6 +14,7 @@ use rustc_middle::ty::{ }; use rustc_middle::{bug, span_bug}; use rustc_session::lint; +use rustc_span::def_id::LocalModId; use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span}; use crate::constructor::Constructor::*; @@ -84,7 +84,7 @@ pub struct RustcPatCtxt<'p, 'tcx: 'p> { /// inhabited can depend on whether it was defined in the current module or /// not. E.g., `struct Foo { _private: ! }` cannot be seen to be empty /// outside its module and should not be matchable with an empty match statement. - pub module: DefId, + pub module: LocalModId, pub typing_env: ty::TypingEnv<'tcx>, /// To allocate the result of `self.ctor_sub_tys()` pub dropless_arena: &'p DroplessArena, diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 0e7dfe18d1048..29ee27bc102f9 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -405,9 +405,8 @@ impl VisibilityLike for EffectiveVisibility { ) -> Self { let effective_vis = find.effective_visibilities.effective_vis(def_id).copied().unwrap_or_else(|| { - let private_vis = ty::Visibility::Restricted( - find.tcx.parent_module_from_def_id(def_id).to_local_def_id(), - ); + let private_vis = + ty::Visibility::Restricted(find.tcx.parent_module_from_def_id(def_id)); EffectiveVisibility::from_vis(private_vis) }); @@ -517,7 +516,6 @@ impl<'tcx> EmbargoVisitor<'tcx> { max_vis: Option, level: Level, ) -> bool { - // FIXME(typed_def_id): Make `Visibility::Restricted` use a `LocalModId` by default. let private_vis = ty::Visibility::Restricted(self.tcx.parent_module_from_def_id(def_id).into()); if max_vis != Some(private_vis) { @@ -1434,12 +1432,10 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> { if self.hard_error && self.required_visibility.greater_than(vis, self.tcx) { let vis_descr = match vis { ty::Visibility::Public => "public", - ty::Visibility::Restricted(vis_def_id) => { - if vis_def_id - == self.tcx.parent_module_from_def_id(local_def_id).to_local_def_id() - { + ty::Visibility::Restricted(vis_mod_id) => { + if vis_mod_id == self.tcx.parent_module_from_def_id(local_def_id) { "private" - } else if vis_def_id.is_top_level_module() { + } else if vis_mod_id.is_top_level_module() { "crate-private" } else { "restricted" diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 3f4c260a496af..98c7af88550dc 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -19,12 +19,13 @@ use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::Attribute; use rustc_hir::attrs::{AttributeKind, MacroUseArgs}; use rustc_hir::def::{self, *}; -use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_index::bit_set::DenseBitSet; use rustc_metadata::creader::LoadedMacro; use rustc_middle::metadata::{ModChild, Reexport}; use rustc_middle::ty::{TyCtxtFeed, Visibility}; use rustc_middle::{bug, span_bug}; +use rustc_span::def_id::{CRATE_MOD_ID, ModId}; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind}; use rustc_span::{Ident, Span, Symbol, kw, sym}; use thin_vec::ThinVec; @@ -71,7 +72,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { expn_id: LocalExpnId, ) { let decl = - self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent.to_module())); + self.arenas.new_def_decl(res, vis.to_mod_id(), span, expn_id, Some(parent.to_module())); let ident = IdentKey::new(orig_ident); self.plant_decl_into_local_module(ident, orig_ident.span, ns, decl); } @@ -274,7 +275,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { Ok(Visibility::Public) } _ => { - let vis = Visibility::Restricted(res.def_id()); + let vis = + Visibility::Restricted(ModId::new_unchecked(res.def_id())); if self.is_accessible_from(vis, parent_scope.module) { if finalize { self.record_partial_res(id, PartialRes::new(res)); @@ -904,7 +906,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { let mut ctor_vis = if vis.is_public() && ast::attr::contains_name(&item.attrs, sym::non_exhaustive) { - Visibility::Restricted(CRATE_DEF_ID) + Visibility::Restricted(CRATE_MOD_ID) } else { vis }; @@ -922,7 +924,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { if ctor_vis.greater_than(field_vis, self.r.tcx) { ctor_vis = field_vis; } - field_visibilities.push(field_vis.to_def_id()); + field_visibilities.push(field_vis.to_mod_id()); } // If this is a unit or tuple-like struct, register the constructor. let feed = self.create_def( @@ -940,7 +942,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields()); let ctor = - StructCtor { res: ctor_res, vis: ctor_vis.to_def_id(), field_visibilities }; + StructCtor { res: ctor_res, vis: ctor_vis.to_mod_id(), field_visibilities }; self.r.struct_ctors.insert(local_def_id, ctor); } self.r.struct_generics.insert(local_def_id, generics.clone()); @@ -1154,7 +1156,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { root_span: span, span, module_path: Vec::new(), - vis: Visibility::Restricted(CRATE_DEF_ID), + vis: Visibility::Restricted(CRATE_MOD_ID), vis_span: item.vis.span, on_unknown_attr: OnUnknownData::from_attrs(this.r, &item.attrs), }) @@ -1310,11 +1312,11 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { let vis = if is_macro_export { Visibility::Public } else { - Visibility::Restricted(CRATE_DEF_ID) + Visibility::Restricted(CRATE_MOD_ID) }; let decl = self.r.arenas.new_def_decl( res, - vis.to_def_id(), + vis.to_mod_id(), span, expansion, Some(parent_scope.module), @@ -1516,7 +1518,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { // If the variant is marked as non_exhaustive then lower the visibility to within the crate. let ctor_vis = if vis.is_public() && ast::attr::contains_name(&variant.attrs, sym::non_exhaustive) { - Visibility::Restricted(CRATE_DEF_ID) + Visibility::Restricted(CRATE_MOD_ID) } else { vis }; diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index b6d278c26e61d..8c3c758f2be69 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -32,6 +32,7 @@ use rustc_session::lint::builtin::{ AMBIGUOUS_PANIC_IMPORTS, MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS, }; use rustc_session::utils::was_invoked_from_cargo; +use rustc_span::def_id::ModId; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edition::Edition; use rustc_span::hygiene::MacroKind; @@ -68,8 +69,8 @@ pub(crate) type LabelSuggestion = (Ident, bool); #[derive(Clone)] pub(crate) struct StructCtor { pub res: Res, - pub vis: Visibility, - pub field_visibilities: Vec>, + pub vis: Visibility, + pub field_visibilities: Vec>, } impl StructCtor { @@ -1991,7 +1992,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } return; } - if Some(parent_nearest) == scope.opt_def_id() { + if Some(parent_nearest.to_def_id()) == scope.opt_def_id() { err.subdiagnostic(MacroDefinedLater { span: unused_ident.span }); err.subdiagnostic(MacroSuggMovePosition { span: ident.span, ident }); return; diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 8f051cf02d41c..90927492195b0 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -7,6 +7,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId}; use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level}; use rustc_middle::ty::Visibility; +use rustc_span::def_id::{CRATE_MOD_ID, LocalModId}; use rustc_span::sym; use tracing::info; @@ -55,7 +56,7 @@ pub(crate) struct EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> { impl Resolver<'_, '_> { fn private_vis_decl(&self, decl: Decl<'_>) -> Visibility { Visibility::Restricted( - decl.parent_module.map_or(CRATE_DEF_ID, |m| m.nearest_parent_mod().expect_local()), + decl.parent_module.map_or(CRATE_MOD_ID, |m| m.nearest_parent_mod().expect_local()), ) } @@ -65,8 +66,8 @@ impl Resolver<'_, '_> { .get_nearest_non_block_module(def_id.to_def_id()) .nearest_parent_mod() .expect_local(); - if normal_mod_id == def_id { - Visibility::Restricted(self.tcx.local_parent(def_id)) + if normal_mod_id.to_local_def_id() == def_id { + Visibility::Restricted(LocalModId::new_unchecked(self.tcx.local_parent(def_id))) } else { Visibility::Restricted(normal_mod_id) } @@ -85,7 +86,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> { r, def_effective_visibilities: Default::default(), import_effective_visibilities: Default::default(), - current_private_vis: Visibility::Restricted(CRATE_DEF_ID), + current_private_vis: Visibility::Restricted(CRATE_MOD_ID), macro_reachable: Default::default(), changed: true, }; @@ -242,7 +243,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> { fn update_macro(&mut self, def_id: LocalDefId, inherited_effective_vis: EffectiveVisibility) { let max_vis = Some(self.r.tcx.local_visibility(def_id)); let priv_vis = if def_id == CRATE_DEF_ID { - Visibility::Restricted(CRATE_DEF_ID) + Visibility::Restricted(CRATE_MOD_ID) } else { self.r.private_vis_def(def_id) }; @@ -364,8 +365,10 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> ), ast::ItemKind::Mod(..) => { - let prev_private_vis = - mem::replace(&mut self.current_private_vis, Visibility::Restricted(def_id)); + let prev_private_vis = mem::replace( + &mut self.current_private_vis, + Visibility::Restricted(LocalModId::new_unchecked(def_id)), + ); self.set_bindings_effective_visibilities(def_id); visit::walk_item(self, item); self.current_private_vis = prev_private_vis; diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 134aec3d0c7d2..26418dff45a5c 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1831,7 +1831,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ) -> PathResult<'ra> { let mut module = None; let mut module_had_parse_errors = !self.mods_with_parse_errors.is_empty() - && self.mods_with_parse_errors.contains(&parent_scope.module.nearest_parent_mod()); + && self + .mods_with_parse_errors + .contains(&parent_scope.module.nearest_parent_mod().to_def_id()); let mut allow_super = true; let mut second_binding = None; diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index d3b1d06b870a9..25f7e28b6738c 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -473,7 +473,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { kind: DeclKind::Import { source_decl: decl, import }, ambiguity: CmCell::new(None), span: import.span, - initial_vis: vis.to_def_id(), + initial_vis: vis.to_mod_id(), ambiguity_vis_max: CmCell::new(None), ambiguity_vis_min: CmCell::new(None), expansion: import.parent_scope.expansion, @@ -1646,7 +1646,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ns: Namespace, ) -> Option { let crate_private_reexport = match decl.vis() { - Visibility::Restricted(def_id) if def_id.is_top_level_module() => true, + Visibility::Restricted(mod_id) if mod_id.is_top_level_module() => true, _ => false, }; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 13576c24e6c08..40cad4241e5a3 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -71,6 +71,7 @@ use rustc_middle::ty::{ use rustc_middle::{bug, span_bug}; use rustc_session::config::CrateType; use rustc_session::lint::builtin::PRIVATE_MACRO_USE; +use rustc_span::def_id::{LocalModId, ModId}; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym}; use smallvec::{SmallVec, smallvec}; @@ -706,7 +707,7 @@ impl<'ra> ModuleData<'ra> { expansion: ExpnId, span: Span, no_implicit_prelude: bool, - vis: Visibility, + vis: Visibility, arenas: &'ra ResolverArenas<'ra>, ) -> Self { let is_foreign = !kind.is_local(); @@ -840,11 +841,11 @@ impl<'ra> Module<'ra> { } } - /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module). + /// The [`ModId`] of the nearest `mod` item ancestor (which may be this module). /// This may be the crate root. - fn nearest_parent_mod(self) -> DefId { + fn nearest_parent_mod(self) -> ModId { match self.kind { - ModuleKind::Def(DefKind::Mod, def_id, _, _) => def_id, + ModuleKind::Def(DefKind::Mod, def_id, _, _) => ModId::new_unchecked(def_id), _ => self.parent.expect("non-root module without parent").nearest_parent_mod(), } } @@ -894,7 +895,7 @@ impl<'ra> LocalModule<'ra> { fn new( parent: Option>, kind: ModuleKind, - vis: Visibility, + vis: Visibility, expn_id: ExpnId, span: Span, no_implicit_prelude: bool, @@ -916,7 +917,7 @@ impl<'ra> ExternModule<'ra> { fn new( parent: Option>, kind: ModuleKind, - vis: Visibility, + vis: Visibility, expn_id: ExpnId, span: Span, no_implicit_prelude: bool, @@ -980,7 +981,7 @@ struct DeclData<'ra> { ambiguity: CmCell, bool /*warning*/)>>, expansion: LocalExpnId, span: Span, - initial_vis: Visibility, + initial_vis: Visibility, /// If the declaration refers to an ambiguous glob set, then this is the most visible /// declaration from the set, if its visibility is different from `initial_vis`. ambiguity_vis_max: CmCell>>, @@ -1099,12 +1100,12 @@ struct AmbiguityError<'ra> { } impl<'ra> DeclData<'ra> { - fn vis(&self) -> Visibility { + fn vis(&self) -> Visibility { // Select the maximum visibility if there are multiple ambiguous glob imports. self.ambiguity_vis_max.get().map(|d| d.vis()).unwrap_or_else(|| self.initial_vis) } - fn min_vis(&self) -> Visibility { + fn min_vis(&self) -> Visibility { // Select the minimum visibility if there are multiple ambiguous glob imports. self.ambiguity_vis_min.get().map(|d| d.vis()).unwrap_or_else(|| self.initial_vis) } @@ -1490,8 +1491,8 @@ pub struct Resolver<'ra, 'tcx> { effective_visibilities: EffectiveVisibilities, macro_reachable_adts: FxIndexMap>, - doc_link_resolutions: FxIndexMap, - doc_link_traits_in_scope: FxIndexMap>, + doc_link_resolutions: FxIndexMap, + doc_link_traits_in_scope: FxIndexMap>, all_macro_rules: UnordSet = Default::default(), /// Invocation ids of all glob delegations. @@ -1539,7 +1540,7 @@ impl<'ra> ResolverArenas<'ra> { fn new_def_decl( &'ra self, res: Res, - vis: Visibility, + vis: Visibility, span: Span, expansion: LocalExpnId, parent_module: Option>, @@ -1924,7 +1925,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } fn feed_visibility(&mut self, feed: TyCtxtFeed<'tcx, LocalDefId>, vis: Visibility) { - feed.visibility(vis.to_def_id()); + feed.visibility(vis.to_mod_id()); self.visibilities_for_hashing.push((feed.def_id(), vis)); } @@ -2359,10 +2360,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } fn resolve_self(&self, ctxt: &mut SyntaxContext, module: Module<'ra>) -> Module<'ra> { - let mut module = self.expect_module(module.nearest_parent_mod()); + let mut module = self.expect_module(module.nearest_parent_mod().to_def_id()); while module.span.ctxt().normalize_to_macros_2_0() != *ctxt { let parent = module.parent.unwrap_or_else(|| self.expn_def_scope(ctxt.remove_mark())); - module = self.expect_module(parent.nearest_parent_mod()); + module = self.expect_module(parent.nearest_parent_mod().to_def_id()); } module } @@ -2749,7 +2750,7 @@ enum Stage { #[derive(Copy, Clone, Debug)] struct ImportSummary { vis: Visibility, - nearest_parent_mod: LocalDefId, + nearest_parent_mod: LocalModId, is_single: bool, priv_macro_use: bool, span: Span, diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 6f204fd6dfd5f..128f20c11b61f 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -28,6 +28,7 @@ use rustc_session::lint::builtin::{ LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_DIAGNOSTIC_ATTRIBUTES, UNUSED_MACRO_RULES, UNUSED_MACROS, }; +use rustc_span::def_id::ModId; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edition::Edition; use rustc_span::hygiene::{self, AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind}; @@ -214,8 +215,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { features: &[Symbol], parent_module_id: Option, ) -> LocalExpnId { - let parent_module = - parent_module_id.map(|module_id| self.owner_def_id(module_id).to_def_id()); + let parent_module = parent_module_id + .map(|module_id| ModId::new_unchecked(self.owner_def_id(module_id).to_def_id())); let expn_id = self.tcx.with_stable_hashing_context(|hcx| { LocalExpnId::fresh( ExpnData::allow_unstable( @@ -230,8 +231,9 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { ) }); - let parent_scope = parent_module - .map_or(self.empty_module, |def_id| self.expect_module(def_id).expect_local()); + let parent_scope = parent_module.map_or(self.empty_module, |mod_id| { + self.expect_module(mod_id.to_def_id()).expect_local() + }); self.ast_transform_scopes.insert(expn_id, parent_scope); expn_id diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index e9de1175561f7..bed824accd5fe 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -490,6 +490,10 @@ impl ModId { self.0.as_local().map(LocalModId::new_unchecked) } + pub fn expect_local(self) -> LocalModId { + LocalModId::new_unchecked(self.0.expect_local()) + } + pub fn is_top_level_module(self) -> bool { self.0.is_top_level_module() } @@ -530,6 +534,10 @@ impl LocalModId { self.0.into() } + pub fn to_mod_id(self) -> ModId { + ModId::new_unchecked(self.0.to_def_id()) + } + #[inline] pub fn to_local_def_id(self) -> LocalDefId { self.0 diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 1c742052783cd..8a422c97bc5f7 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -41,7 +41,7 @@ use rustc_macros::{Decodable, Encodable, StableHash}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use tracing::{debug, trace}; -use crate::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, StableCrateId}; +use crate::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, ModId, StableCrateId}; use crate::edition::Edition; use crate::source_map::SourceMap; use crate::symbol::{Symbol, kw, sym}; @@ -1012,7 +1012,7 @@ pub struct ExpnData { /// if this `ExpnData` corresponds to a macro invocation pub macro_def_id: Option, /// The normal module (`mod`) in which the expanded macro was defined. - pub parent_module: Option, + pub parent_module: Option, /// Suppresses the `unsafe_code` lint for code produced by this macro. pub(crate) allow_internal_unsafe: bool, /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro. @@ -1036,7 +1036,7 @@ impl ExpnData { allow_internal_unstable: Option>, edition: Edition, macro_def_id: Option, - parent_module: Option, + parent_module: Option, allow_internal_unsafe: bool, local_inner_macros: bool, collapse_debuginfo: bool, @@ -1065,7 +1065,7 @@ impl ExpnData { call_site: Span, edition: Edition, macro_def_id: Option, - parent_module: Option, + parent_module: Option, ) -> ExpnData { ExpnData { kind, @@ -1090,7 +1090,7 @@ impl ExpnData { edition: Edition, allow_internal_unstable: Arc<[Symbol]>, macro_def_id: Option, - parent_module: Option, + parent_module: Option, ) -> ExpnData { ExpnData { allow_internal_unstable: Some(allow_internal_unstable), diff --git a/tests/ui/thir-print/thir-tree-field-expr-index.stdout b/tests/ui/thir-print/thir-tree-field-expr-index.stdout index 5bf97a1852904..f1ab57a4545eb 100644 --- a/tests/ui/thir-print/thir-tree-field-expr-index.stdout +++ b/tests/ui/thir-print/thir-tree-field-expr-index.stdout @@ -85,7 +85,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -230,7 +230,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -317,7 +317,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -404,7 +404,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -491,7 +491,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -578,7 +578,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -665,7 +665,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } @@ -773,7 +773,7 @@ body: adt_def: AdtDef { did: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S) - variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(DefId(0:0 ~ thir_tree_field_expr_index[5059])), safety: Safe, value: None }], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:3 ~ thir_tree_field_expr_index[5059]::S), ctor: None, name: "S", discr: Relative(0), fields: [FieldDef { did: DefId(0:4 ~ thir_tree_field_expr_index[5059]::S::a), name: "a", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:5 ~ thir_tree_field_expr_index[5059]::S::b), name: "b", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:6 ~ thir_tree_field_expr_index[5059]::S::c), name: "c", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:7 ~ thir_tree_field_expr_index[5059]::S::d), name: "d", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }, FieldDef { did: DefId(0:8 ~ thir_tree_field_expr_index[5059]::S::e), name: "e", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_field_expr_index[5059]))), safety: Safe, value: None }], tainted: None, flags: }] flags: IS_STRUCT repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 7076349371981215213 } } diff --git a/tests/ui/thir-print/thir-tree-match.stdout b/tests/ui/thir-print/thir-tree-match.stdout index 33baafeb48b26..3ef5c3dfce6e5 100644 --- a/tests/ui/thir-print/thir-tree-match.stdout +++ b/tests/ui/thir-print/thir-tree-match.stdout @@ -93,7 +93,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_match[fcf8]))), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 13397682652773712997 } } @@ -157,7 +157,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_match[fcf8]))), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 13397682652773712997 } } @@ -210,7 +210,7 @@ body: adt_def: AdtDef { did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo) - variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] + variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(ModId(DefId(0:0 ~ thir_tree_match[fcf8]))), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }] flags: IS_ENUM repr: ReprOptions { int: None, align: None, pack: None, flags: , scalable: None, field_shuffle_seed: 13397682652773712997 } } From 60d52ca07a8993d814bd4f038bc08c9c247148c9 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 13 Jul 2026 21:23:39 -0500 Subject: [PATCH 5/6] Fix rustdoc with ModId refactors --- compiler/rustc_span/src/def_id.rs | 4 +++ src/librustdoc/clean/inline.rs | 4 +-- src/librustdoc/clean/types.rs | 4 +-- src/librustdoc/clean/utils.rs | 7 +++-- src/librustdoc/html/format.rs | 14 ++++----- src/librustdoc/json/conversions.rs | 15 +++++----- .../passes/collect_intra_doc_links.rs | 29 ++++++++++--------- .../passes/lint/redundant_explicit_links.rs | 4 +-- .../clippy/clippy_lints/src/inherent_impl.rs | 4 +-- src/tools/clippy/clippy_utils/src/lib.rs | 6 ++-- 10 files changed, 49 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index bed824accd5fe..43a0d18cde435 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -494,6 +494,10 @@ impl ModId { LocalModId::new_unchecked(self.0.expect_local()) } + pub fn is_crate_root(self) -> bool { + self.0.is_crate_root() + } + pub fn is_top_level_module(self) -> bool { self.0.is_top_level_module() } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index d4cdc7781fed5..46b8683137c60 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::thin_vec::{ThinVec, thin_vec}; use rustc_hir::def::{DefKind, MacroKinds, Res}; -use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModId}; use rustc_hir::{self as hir, Mutability, find_attr}; use rustc_metadata::creader::{CStore, LoadedMacro}; use rustc_middle::ty::fast_reject::SimplifiedType; @@ -181,7 +181,7 @@ pub(crate) fn try_inline( pub(crate) fn try_inline_glob( cx: &mut DocContext<'_>, res: Res, - current_mod: LocalModDefId, + current_mod: LocalModId, visited: &mut DefIdSet, inlined_names: &mut FxHashSet<(ItemType, Symbol)>, import: &hir::Item<'_>, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 620951bb4974b..3034ce5fb318c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -25,7 +25,7 @@ use rustc_resolve::rustdoc::{ DocFragment, add_doc_fragment, attrs_to_doc_fragments, inner_docs, span_of_fragments, }; use rustc_session::Session; -use rustc_span::def_id::CRATE_DEF_ID; +use rustc_span::def_id::{CRATE_DEF_ID, ModId}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{Symbol, kw, sym}; use rustc_span::{DUMMY_SP, FileName, Ident, Loc, RemapPathScopeComponents}; @@ -871,7 +871,7 @@ impl Item { /// Returns the visibility of the current item. If the visibility is "inherited", then `None` /// is returned. - pub(crate) fn visibility(&self, tcx: TyCtxt<'_>) -> Option> { + pub(crate) fn visibility(&self, tcx: TyCtxt<'_>) -> Option> { let def_id = match self.item_id { // Anything but DefId *shouldn't* matter, but return a reasonable value anyway. ItemId::Auto { .. } | ItemId::Blanket { .. } => return None, diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index f77e201805a2b..9290555f2ef39 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -16,6 +16,7 @@ use rustc_hir::find_attr; use rustc_metadata::rendered_const; use rustc_middle::mir; use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, TyCtxt, TypeVisitableExt}; +use rustc_span::def_id::ModId; use rustc_span::symbol::{Symbol, kw, sym}; use tracing::{debug, warn}; @@ -556,17 +557,17 @@ where } /// Find the nearest parent module of a [`DefId`]. -pub(crate) fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { +pub(crate) fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { if def_id.is_top_level_module() { // The crate root has no parent. Use it as the root instead. - Some(def_id) + Some(ModId::new_unchecked(def_id)) } else { let mut current = def_id; // The immediate parent might not always be a module. // Find the first parent which is. while let Some(parent) = tcx.opt_parent(current) { if tcx.def_kind(parent) == DefKind::Mod { - return Some(parent); + return Some(ModId::new_unchecked(parent)); } current = parent; } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 8c225da583bf2..4b9bf8bbd3a07 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1421,29 +1421,29 @@ pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) match vis { ty::Visibility::Public => f.write_str("pub ")?, - ty::Visibility::Restricted(vis_did) => { + ty::Visibility::Restricted(vis_mod_id) => { // FIXME(camelid): This may not work correctly if `item_did` is a module. // However, rustdoc currently never displays a module's // visibility, so it shouldn't matter. let parent_module = find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id()); - if vis_did.is_crate_root() { + if vis_mod_id.is_crate_root() { f.write_str("pub(crate) ")?; - } else if parent_module == Some(vis_did) { + } else if parent_module == Some(vis_mod_id) { // `pub(in foo)` where `foo` is the parent module // is the same as no visibility modifier; do nothing } else if parent_module - .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent)) - == Some(vis_did) + .and_then(|parent| find_nearest_parent_module(cx.tcx(), parent.to_def_id())) + == Some(vis_mod_id) { f.write_str("pub(super) ")?; } else { - let path = cx.tcx().def_path(vis_did); + let path = cx.tcx().def_path(vis_mod_id.to_def_id()); debug!("path={path:?}"); // modified from `resolved_path()` to work with `DefPathData` let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); - let anchor = print_anchor(vis_did, last_name, cx); + let anchor = print_anchor(vis_mod_id.to_def_id(), last_name, cx); f.write_str("pub(in ")?; for seg in &path.data[..path.data.len() - 1] { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 740a69d3c6b7a..470ac0c8a5c86 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -16,6 +16,7 @@ use rustc_hir::{HeaderSafety, Safety, find_attr}; use rustc_metadata::rendered_const; use rustc_middle::ty::TyCtxt; use rustc_middle::{bug, ty}; +use rustc_span::def_id::ModId; use rustc_span::{Pos, Symbol, kw, sym}; use rustdoc_json_types::*; @@ -207,15 +208,15 @@ impl FromClean for Option { } } -impl FromClean>> for Visibility { - fn from_clean(v: &Option>, renderer: &JsonRenderer<'_>) -> Self { - match v { +impl FromClean>> for Visibility { + fn from_clean(v: &Option>, renderer: &JsonRenderer<'_>) -> Self { + match *v { None => Visibility::Default, Some(ty::Visibility::Public) => Visibility::Public, - Some(ty::Visibility::Restricted(did)) if did.is_crate_root() => Visibility::Crate, - Some(ty::Visibility::Restricted(did)) => Visibility::Restricted { - parent: renderer.id_from_item_default((*did).into()), - path: renderer.tcx.def_path(*did).to_string_no_crate_verbose(), + Some(ty::Visibility::Restricted(mod_id)) if mod_id.is_crate_root() => Visibility::Crate, + Some(ty::Visibility::Restricted(mod_id)) => Visibility::Restricted { + parent: renderer.id_from_item_default(ItemId::DefId(mod_id.to_def_id())), + path: renderer.tcx.def_path(mod_id.to_def_id()).to_string_no_crate_verbose(), }, } } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 02423c4b9391b..ce191aaf445d5 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -26,6 +26,7 @@ use rustc_resolve::rustdoc::{ use rustc_session::config::CrateType; use rustc_session::lint::Lint; use rustc_span::BytePos; +use rustc_span::def_id::ModId; use rustc_span::symbol::{Ident, Symbol, sym}; use smallvec::{SmallVec, smallvec}; use tracing::{debug, info, instrument, trace}; @@ -170,7 +171,7 @@ struct UnresolvedPath<'a> { /// Item on which the link is resolved, used for resolving `Self`. item_id: DefId, /// The scope the link was resolved in. - module_id: DefId, + module_id: ModId, /// If part of the link resolved, this has the `Res`. /// /// In `[std::io::Error::x]`, `std::io::Error` would be a partial resolution. @@ -208,7 +209,7 @@ pub(crate) enum UrlFragment { #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub(crate) struct ResolutionInfo { item_id: DefId, - module_id: DefId, + module_id: ModId, dis: Option, path_str: Box, extra_fragment: Option, @@ -286,7 +287,7 @@ impl<'tcx> LinkCollector<'_, 'tcx> { &self, path_str: &'path str, item_id: DefId, - module_id: DefId, + module_id: ModId, ) -> Result<(Res, DefId), UnresolvedPath<'path>> { let tcx = self.cx.tcx; let no_res = || UnresolvedPath { @@ -355,7 +356,7 @@ impl<'tcx> LinkCollector<'_, 'tcx> { path_str: &str, ns: Namespace, item_id: DefId, - module_id: DefId, + module_id: ModId, ) -> Option { if let res @ Some(..) = resolve_self_ty(self.cx.tcx, path_str, ns, item_id) { return res; @@ -392,7 +393,7 @@ impl<'tcx> LinkCollector<'_, 'tcx> { ns: Namespace, disambiguator: Option, item_id: DefId, - module_id: DefId, + module_id: ModId, ) -> Result)>, UnresolvedPath<'path>> { let tcx = self.cx.tcx; @@ -604,7 +605,7 @@ fn resolve_associated_item<'tcx>( item_name: Symbol, ns: Namespace, disambiguator: Option, - module_id: DefId, + module_id: ModId, ) -> Vec<(Res, DefId)> { let item_ident = Ident::with_dummy_span(item_name); @@ -665,7 +666,7 @@ fn resolve_assoc_on_primitive<'tcx>( prim: PrimitiveType, ns: Namespace, item_ident: Ident, - module_id: DefId, + module_id: ModId, ) -> Vec<(Res, DefId)> { let root_res = Res::Primitive(prim); let items = resolve_primitive_inherent_assoc_item(tcx, prim, ns, item_ident); @@ -690,7 +691,7 @@ fn resolve_assoc_on_adt<'tcx>( item_ident: Ident, ns: Namespace, disambiguator: Option, - module_id: DefId, + module_id: ModId, ) -> Vec<(Res, DefId)> { debug!("looking for associated item named {item_ident} for item {adt_def_id:?}"); let root_res = Res::from_def_id(tcx, adt_def_id); @@ -735,7 +736,7 @@ fn resolve_assoc_on_simple_type<'tcx>( ty_def_id: DefId, item_ident: Ident, ns: Namespace, - module_id: DefId, + module_id: ModId, ) -> Vec<(Res, DefId)> { let root_res = Res::from_def_id(tcx, ty_def_id); // Checks if item_name belongs to `impl SomeItem` @@ -781,7 +782,7 @@ fn resolve_structfield<'tcx>(adt_def: ty::AdtDef<'tcx>, item_name: Symbol) -> Op /// `::source`. fn resolve_associated_trait_item<'tcx>( ty: Ty<'tcx>, - module: DefId, + module: ModId, item_ident: Ident, ns: Namespace, tcx: TyCtxt<'tcx>, @@ -841,7 +842,7 @@ fn trait_assoc_to_impl_assoc_item<'tcx>( fn trait_impls_for<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, - module: DefId, + module: ModId, ) -> FxIndexSet<(DefId, DefId)> { let mut impls = FxIndexSet::default(); @@ -1097,7 +1098,7 @@ impl LinkCollector<'_, '_> { return; } let module_id = match tcx.def_kind(item_id) { - DefKind::Mod if item.inner_docs(tcx) => item_id, + DefKind::Mod if item.inner_docs(tcx) => ModId::new_unchecked(item_id), _ => find_nearest_parent_module(tcx, item_id).unwrap(), }; for md_link in preprocessed_markdown_links(&doc) { @@ -1180,7 +1181,7 @@ impl LinkCollector<'_, '_> { dox: &str, item: &Item, item_id: DefId, - module_id: DefId, + module_id: ModId, PreprocessedMarkdownLink(pp_link, ori_link): &PreprocessedMarkdownLink, ) -> Option { trace!("considering link '{}'", ori_link.link); @@ -2112,7 +2113,7 @@ fn resolution_failure( } let last_found_module = match *partial_res { - Some(Res::Def(DefKind::Mod, id)) => Some(id), + Some(Res::Def(DefKind::Mod, id)) => Some(ModId::new_unchecked(id)), None => Some(module_id), _ => None, }; diff --git a/src/librustdoc/passes/lint/redundant_explicit_links.rs b/src/librustdoc/passes/lint/redundant_explicit_links.rs index cd7b7caac69ad..22a79ecf6a53e 100644 --- a/src/librustdoc/passes/lint/redundant_explicit_links.rs +++ b/src/librustdoc/passes/lint/redundant_explicit_links.rs @@ -9,7 +9,7 @@ use rustc_resolve::rustdoc::pulldown_cmark::{ BrokenLink, BrokenLinkCallback, CowStr, Event, LinkType, OffsetIter, Parser, Tag, }; use rustc_resolve::rustdoc::{prepare_to_doc_link_resolution, source_span_for_markdown_range}; -use rustc_span::def_id::DefId; +use rustc_span::def_id::{DefId, ModId}; use rustc_span::{Span, Symbol}; use crate::clean::Item; @@ -58,7 +58,7 @@ fn check_redundant_explicit_link_for_did( } let module_id = match cx.tcx.def_kind(did) { - DefKind::Mod if item.inner_docs(cx.tcx) => did, + DefKind::Mod if item.inner_docs(cx.tcx) => ModId::new_unchecked(did), _ => find_nearest_parent_module(cx.tcx, did).unwrap(), }; diff --git a/src/tools/clippy/clippy_lints/src/inherent_impl.rs b/src/tools/clippy/clippy_lints/src/inherent_impl.rs index 257a165ba8315..7475897852177 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_impl.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_impl.rs @@ -3,7 +3,7 @@ use clippy_config::types::InherentImplLintScope; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::{fulfill_or_allowed, is_cfg_test, is_in_cfg_test}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::def_id::{LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{LocalDefId, LocalModId}; use rustc_hir::{Item, ItemKind, Node}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::impl_lint_pass; @@ -63,7 +63,7 @@ impl MultipleInherentImpl { #[derive(Hash, Eq, PartialEq, Clone)] enum Criterion { - Module(LocalModDefId), + Module(LocalModId), File(FileName), Crate, } diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index 56a5e80df2df8..2be96a6f08973 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -88,7 +88,7 @@ use rustc_data_structures::unhash::UnindexMap; use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk}; use rustc_hir::attrs::CfgEntry; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModId}; use rustc_hir::definitions::{DefPath, DefPathData}; use rustc_hir::hir_id::{HirIdMap, HirIdSet}; use rustc_hir::intravisit::{Visitor, walk_expr}; @@ -2348,11 +2348,11 @@ pub fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool { false } -static TEST_ITEM_NAMES_CACHE: OnceLock>>> = OnceLock::new(); +static TEST_ITEM_NAMES_CACHE: OnceLock>>> = OnceLock::new(); /// Returns the names of the test items in the given module. /// The names are sorted using the default `Symbol` ordering. -fn test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId) -> Vec { +fn test_item_names(tcx: TyCtxt<'_>, module: LocalModId) -> Vec { let cache = TEST_ITEM_NAMES_CACHE.get_or_init(|| Mutex::new(FxHashMap::default())); let mut map = cache.lock().unwrap(); match map.entry(module) { From 2c70f93ce7c07b82fc51969f6bb12a48e7be2d27 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 13 Jul 2026 21:23:39 -0500 Subject: [PATCH 6/6] Fix clippy with ModId refactors --- src/tools/clippy/clippy_lints/src/error_impl_error.rs | 4 ++-- src/tools/clippy/clippy_lints/src/infallible_try_from.rs | 2 +- src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs | 4 ++-- src/tools/clippy/clippy_lints/src/unused_trait_names.rs | 2 +- src/tools/clippy/clippy_lints/src/wildcard_imports.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/error_impl_error.rs b/src/tools/clippy/clippy_lints/src/error_impl_error.rs index 4b9e3cee011c6..4b7aaa34d1ad8 100644 --- a/src/tools/clippy/clippy_lints/src/error_impl_error.rs +++ b/src/tools/clippy/clippy_lints/src/error_impl_error.rs @@ -81,7 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError { /// which aren't reexported fn is_visible_outside_module(cx: &LateContext<'_>, def_id: LocalDefId) -> bool { !matches!( - cx.tcx.visibility(def_id), - Visibility::Restricted(mod_def_id) if cx.tcx.parent_module_from_def_id(def_id).to_def_id() == mod_def_id + cx.tcx.local_visibility(def_id), + Visibility::Restricted(mod_def_id) if cx.tcx.parent_module_from_def_id(def_id) == mod_def_id ) } diff --git a/src/tools/clippy/clippy_lints/src/infallible_try_from.rs b/src/tools/clippy/clippy_lints/src/infallible_try_from.rs index b7cbe667b3346..7472ec0a2106b 100644 --- a/src/tools/clippy/clippy_lints/src/infallible_try_from.rs +++ b/src/tools/clippy/clippy_lints/src/infallible_try_from.rs @@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for InfallibleTryFrom { .filter_by_name_unhygienic_and_kind(sym::Error, AssocTag::Type) { let ii_ty = cx.tcx.type_of(ii.def_id).instantiate_identity().skip_norm_wip(); - if !ii_ty.is_inhabited_from(cx.tcx, ii.def_id, cx.typing_env()) { + if !ii_ty.is_inhabited_from(cx.tcx, cx.tcx.parent_module_from_def_id(ii.def_id.expect_local()), cx.typing_env()) { let mut span = MultiSpan::from_span(cx.tcx.def_span(item.owner_id.to_def_id())); let ii_ty_span = cx .tcx diff --git a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs index c7f7b47403376..8abb5159f90e6 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs @@ -5,7 +5,7 @@ use rustc_hir::{Item, ItemKind, UseKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; use rustc_session::impl_lint_pass; -use rustc_span::def_id::CRATE_DEF_ID; +use rustc_span::def_id::CRATE_MOD_ID; declare_clippy_lint! { /// ### What it does @@ -44,7 +44,7 @@ pub struct RedundantPubCrate { impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { - if cx.tcx.visibility(item.owner_id.def_id) == ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) + if cx.tcx.local_visibility(item.owner_id.def_id) == ty::Visibility::Restricted(CRATE_MOD_ID) && !cx.effective_visibilities.is_exported(item.owner_id.def_id) && self.is_exported.last() == Some(&false) && !is_ignorable_export(item) diff --git a/src/tools/clippy/clippy_lints/src/unused_trait_names.rs b/src/tools/clippy/clippy_lints/src/unused_trait_names.rs index be41bdd380204..e09018284434f 100644 --- a/src/tools/clippy/clippy_lints/src/unused_trait_names.rs +++ b/src/tools/clippy/clippy_lints/src/unused_trait_names.rs @@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedTraitNames { && cx.tcx.resolutions(()).maybe_unused_trait_imports.contains(&item.owner_id.def_id) // Only check this import if it is visible to its module only (no pub, pub(crate), ...) && let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id) - && cx.tcx.visibility(item.owner_id.def_id) == Visibility::Restricted(module.to_def_id()) + && cx.tcx.local_visibility(item.owner_id.def_id) == Visibility::Restricted(module) && let Some(last_segment) = path.segments.last() && let Some(snip) = snippet_opt(cx, last_segment.ident.span) && self.msrv.meets(cx, msrvs::UNDERSCORE_IMPORTS) diff --git a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs index 22c46527b5c6c..19ab3789232cd 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs @@ -123,7 +123,7 @@ impl LateLintPass<'_> for WildcardImports { } let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id); - if cx.tcx.visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module.to_def_id()) + if cx.tcx.local_visibility(item.owner_id.def_id) != ty::Visibility::Restricted(module) && !self.warn_on_all { return;