From 11751ce2976fc105902e63d88191dacf9b0c5601 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 3 Jul 2025 13:23:11 +0000 Subject: [PATCH] Pre-hash visibilities in resolver. --- compiler/rustc_middle/src/hir/map.rs | 6 +----- compiler/rustc_middle/src/ty/mod.rs | 3 ++- compiler/rustc_resolve/src/lib.rs | 10 +++++++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 291707878a342..3ee1bdb73d867 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -1177,11 +1177,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh { } tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher); tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher); - // Hash visibility information since it does not appear in HIR. - // FIXME: Figure out how to remove `visibilities_for_hashing` by hashing visibilities on - // the fly in the resolver, storing only their accumulated hash in `ResolverGlobalCtxt`, - // and combining it with other hashes here. - resolutions.visibilities_for_hashing.hash_stable(&mut hcx, &mut stable_hasher); + resolutions.visibilities_hash.hash_stable(&mut hcx, &mut stable_hasher); with_metavar_spans(|mspans| { mspans.freeze_and_get_read_spans().hash_stable(&mut hcx, &mut stable_hasher); }); diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 7f79cd5d4688e..7243a8371d698 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -29,6 +29,7 @@ use rustc_ast::expand::StrippedCfgItem; use rustc_ast::node_id::NodeMap; pub use rustc_ast_ir::{Movability, Mutability, try_visit}; use rustc_attr_data_structures::AttributeKind; +use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::intern::Interned; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; @@ -178,7 +179,7 @@ pub struct ResolverOutputs { #[derive(Debug, HashStable)] pub struct ResolverGlobalCtxt { - pub visibilities_for_hashing: Vec<(LocalDefId, Visibility)>, + pub visibilities_hash: Fingerprint, /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. pub expn_that_defined: UnordMap, pub effective_visibilities: EffectiveVisibilities, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 7162f3a77d350..f31fbda8bacdb 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -44,6 +44,7 @@ use rustc_ast::{ }; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::intern::Interned; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::steal::Steal; use rustc_data_structures::sync::FreezeReadGuard; use rustc_data_structures::unord::{UnordMap, UnordSet}; @@ -1644,6 +1645,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } pub fn into_outputs(self) -> ResolverOutputs { + let visibilities_hash = { + let mut hasher = StableHasher::new(); + let mut hcx = self.create_stable_hashing_context(); + self.visibilities_for_hashing.hash_stable(&mut hcx, &mut hasher); + hasher.finish() + }; + let proc_macros = self.proc_macros; let expn_that_defined = self.expn_that_defined; let extern_crate_map = self.extern_crate_map; @@ -1665,7 +1673,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let global_ctxt = ResolverGlobalCtxt { expn_that_defined, - visibilities_for_hashing: self.visibilities_for_hashing, + visibilities_hash, effective_visibilities, extern_crate_map, module_children: self.module_children,