Rollup merge of #144822 - Zalathar:hash-owner-nodes, r=compiler-errors

Return a struct with named fields from `hash_owner_nodes`

While looking through this code for other reasons, I noticed a nice opportunity to return a struct with named fields instead of a tuple. The first patch also introduces an early-return to flatten the rest of `hash_owner_nodes`.

There are further changes that could potentially be made here (renaming things, `Option<Hashes>` instead of optional fields), but I'm not deeply familiar with this code so I didn't want to disturb the calling code too much.
This commit is contained in:
Samuel Tardieu
2025-08-03 21:56:59 +02:00
committed by GitHub
3 changed files with 44 additions and 28 deletions

View File

@@ -675,7 +675,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let bodies = SortedMap::from_presorted_elements(bodies); let bodies = SortedMap::from_presorted_elements(bodies);
// Don't hash unless necessary, because it's expensive. // Don't hash unless necessary, because it's expensive.
let (opt_hash_including_bodies, attrs_hash, delayed_lints_hash) = let rustc_middle::hir::Hashes { opt_hash_including_bodies, attrs_hash, delayed_lints_hash } =
self.tcx.hash_owner_nodes(node, &bodies, &attrs, &delayed_lints, define_opaque); self.tcx.hash_owner_nodes(node, &bodies, &attrs, &delayed_lints, define_opaque);
let num_nodes = self.item_local_id_counter.as_usize(); let num_nodes = self.item_local_id_counter.as_usize();
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes); let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);

View File

@@ -174,8 +174,15 @@ impl<'tcx> TyCtxt<'tcx> {
attrs: &SortedMap<ItemLocalId, &[Attribute]>, attrs: &SortedMap<ItemLocalId, &[Attribute]>,
delayed_lints: &[DelayedLint], delayed_lints: &[DelayedLint],
define_opaque: Option<&[(Span, LocalDefId)]>, define_opaque: Option<&[(Span, LocalDefId)]>,
) -> (Option<Fingerprint>, Option<Fingerprint>, Option<Fingerprint>) { ) -> Hashes {
if self.needs_crate_hash() { if !self.needs_crate_hash() {
return Hashes {
opt_hash_including_bodies: None,
attrs_hash: None,
delayed_lints_hash: None,
};
}
self.with_stable_hashing_context(|mut hcx| { self.with_stable_hashing_context(|mut hcx| {
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
node.hash_stable(&mut hcx, &mut stable_hasher); node.hash_stable(&mut hcx, &mut stable_hasher);
@@ -196,12 +203,21 @@ impl<'tcx> TyCtxt<'tcx> {
delayed_lints.hash_stable(&mut hcx, &mut stable_hasher); delayed_lints.hash_stable(&mut hcx, &mut stable_hasher);
let h3 = stable_hasher.finish(); let h3 = stable_hasher.finish();
(Some(h1), Some(h2), Some(h3)) Hashes {
opt_hash_including_bodies: Some(h1),
attrs_hash: Some(h2),
delayed_lints_hash: Some(h3),
}
}) })
} else {
(None, None, None)
} }
} }
/// Hashes computed by [`TyCtxt::hash_owner_nodes`] if necessary.
#[derive(Clone, Copy, Debug)]
pub struct Hashes {
pub opt_hash_including_bodies: Option<Fingerprint>,
pub attrs_hash: Option<Fingerprint>,
pub delayed_lints_hash: Option<Fingerprint>,
} }
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {

View File

@@ -1381,7 +1381,7 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
let bodies = Default::default(); let bodies = Default::default();
let attrs = hir::AttributeMap::EMPTY; let attrs = hir::AttributeMap::EMPTY;
let (opt_hash_including_bodies, _, _) = let rustc_middle::hir::Hashes { opt_hash_including_bodies, .. } =
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, &[], attrs.define_opaque); self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, &[], attrs.define_opaque);
let node = node.into(); let node = node.into();
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes { self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {