From df4ffb9f2272c3c13b833ba7b0f20a97e0166c39 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 3683d1e251cb7..2cb517f52f36f 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -1178,11 +1178,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 d07ecdaf3e4ac..9c573fb36c139 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -27,6 +27,7 @@ pub use intrinsic::IntrinsicDef; use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, VariantIdx}; use rustc_ast::node_id::NodeMap; pub use rustc_ast_ir::{Movability, Mutability, try_visit}; +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}; @@ -170,7 +171,7 @@ mod visit; #[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 dbde6f7cfd7ed..552c4dcda789a 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -43,6 +43,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, FreezeWriteGuard}; use rustc_data_structures::unord::{UnordMap, UnordSet}; @@ -1701,6 +1702,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; @@ -1722,7 +1730,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,