diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 94e1e06a954..9f63adb1e29 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -642,23 +642,8 @@ fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInf let bodies = SortedMap::from_presorted_elements(bodies); // Don't hash unless necessary, because it's expensive. - let (opt_hash_including_bodies, attrs_hash) = if self.tcx.needs_crate_hash() { - self.tcx.with_stable_hashing_context(|mut hcx| { - let mut stable_hasher = StableHasher::new(); - node.hash_stable(&mut hcx, &mut stable_hasher); - // Bodies are stored out of line, so we need to pull them explicitly in the hash. - bodies.hash_stable(&mut hcx, &mut stable_hasher); - let h1 = stable_hasher.finish(); - - let mut stable_hasher = StableHasher::new(); - attrs.hash_stable(&mut hcx, &mut stable_hasher); - let h2 = stable_hasher.finish(); - - (Some(h1), Some(h2)) - }) - } else { - (None, None) - }; + let (opt_hash_including_bodies, attrs_hash) = + self.tcx.hash_owner_nodes(node, &bodies, &attrs); let num_nodes = self.item_local_id_counter.as_usize(); let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes); let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies }; diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 61bdb5d4bb7..f9fa8ac2f7a 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -8,6 +8,9 @@ use crate::query::Providers; use crate::ty::{EarlyBinder, ImplSubject, TyCtxt}; +use rustc_data_structures::fingerprint::Fingerprint; +use rustc_data_structures::sorted_map::SortedMap; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::{try_par_for_each_in, DynSend, DynSync}; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; @@ -121,6 +124,30 @@ pub fn is_foreign_item(self, def_id: impl Into) -> bool { self.opt_parent(def_id.into()) .is_some_and(|parent| matches!(self.def_kind(parent), DefKind::ForeignMod)) } + + pub fn hash_owner_nodes( + self, + node: OwnerNode<'_>, + bodies: &SortedMap>, + attrs: &SortedMap, + ) -> (Option, Option) { + if self.needs_crate_hash() { + self.with_stable_hashing_context(|mut hcx| { + let mut stable_hasher = StableHasher::new(); + node.hash_stable(&mut hcx, &mut stable_hasher); + // Bodies are stored out of line, so we need to pull them explicitly in the hash. + bodies.hash_stable(&mut hcx, &mut stable_hasher); + let h1 = stable_hasher.finish(); + + let mut stable_hasher = StableHasher::new(); + attrs.hash_stable(&mut hcx, &mut stable_hasher); + let h2 = stable_hasher.finish(); + (Some(h1), Some(h2)) + }) + } else { + (None, None) + } + } } pub fn provide(providers: &mut Providers) { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 3f628092190..4bcbf1c0374 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -240,8 +240,14 @@ fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) { fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) { feed.local_def_id_to_hir_id(HirId::make_owner(feed.def_id())); + + let node = hir::OwnerNode::AssocOpaqueTy(&hir::AssocOpaqueTy {}); + let bodies = Default::default(); + let attrs = hir::AttributeMap::EMPTY; + + let (opt_hash_including_bodies, _) = feed.tcx.hash_owner_nodes(node, &bodies, &attrs.map); feed.opt_hir_owner_nodes(Some(feed.tcx.arena.alloc(hir::OwnerNodes { - opt_hash_including_bodies: None, + opt_hash_including_bodies, nodes: IndexVec::from_elem_n( hir::ParentedNode { parent: hir::ItemLocalId::INVALID, @@ -249,9 +255,9 @@ fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) { }, 1, ), - bodies: Default::default(), + bodies, }))); - feed.feed_owner_id().hir_attrs(hir::AttributeMap::EMPTY); + feed.feed_owner_id().hir_attrs(attrs); } /// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated diff --git a/tests/ui/async-await/in-trait/hir-hash.rs b/tests/ui/async-await/in-trait/hir-hash.rs new file mode 100644 index 00000000000..8324fec8282 --- /dev/null +++ b/tests/ui/async-await/in-trait/hir-hash.rs @@ -0,0 +1,11 @@ +// Issue #122508 + +//@ check-pass +//@ incremental +//@ edition:2021 + +trait MyTrait { + async fn bar(&self) -> i32; +} + +fn main() {}