Skip to content

Commit ee03c28

Browse files
committed
Auto merge of #122517 - petrochenkov:bodihash, r=oli-obk
Fill in HIR hash for associated opaque types Fixes #122508
2 parents c2901f5 + ef5513f commit ee03c28

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -642,23 +642,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
642642
let bodies = SortedMap::from_presorted_elements(bodies);
643643

644644
// Don't hash unless necessary, because it's expensive.
645-
let (opt_hash_including_bodies, attrs_hash) = if self.tcx.needs_crate_hash() {
646-
self.tcx.with_stable_hashing_context(|mut hcx| {
647-
let mut stable_hasher = StableHasher::new();
648-
node.hash_stable(&mut hcx, &mut stable_hasher);
649-
// Bodies are stored out of line, so we need to pull them explicitly in the hash.
650-
bodies.hash_stable(&mut hcx, &mut stable_hasher);
651-
let h1 = stable_hasher.finish();
652-
653-
let mut stable_hasher = StableHasher::new();
654-
attrs.hash_stable(&mut hcx, &mut stable_hasher);
655-
let h2 = stable_hasher.finish();
656-
657-
(Some(h1), Some(h2))
658-
})
659-
} else {
660-
(None, None)
661-
};
645+
let (opt_hash_including_bodies, attrs_hash) =
646+
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
662647
let num_nodes = self.item_local_id_counter.as_usize();
663648
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
664649
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };

compiler/rustc_middle/src/hir/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pub mod place;
88

99
use crate::query::Providers;
1010
use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
11+
use rustc_data_structures::fingerprint::Fingerprint;
12+
use rustc_data_structures::sorted_map::SortedMap;
13+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1114
use rustc_data_structures::sync::{try_par_for_each_in, DynSend, DynSync};
1215
use rustc_hir::def::DefKind;
1316
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
@@ -121,6 +124,30 @@ impl<'tcx> TyCtxt<'tcx> {
121124
self.opt_parent(def_id.into())
122125
.is_some_and(|parent| matches!(self.def_kind(parent), DefKind::ForeignMod))
123126
}
127+
128+
pub fn hash_owner_nodes(
129+
self,
130+
node: OwnerNode<'_>,
131+
bodies: &SortedMap<ItemLocalId, &Body<'_>>,
132+
attrs: &SortedMap<ItemLocalId, &[rustc_ast::Attribute]>,
133+
) -> (Option<Fingerprint>, Option<Fingerprint>) {
134+
if self.needs_crate_hash() {
135+
self.with_stable_hashing_context(|mut hcx| {
136+
let mut stable_hasher = StableHasher::new();
137+
node.hash_stable(&mut hcx, &mut stable_hasher);
138+
// Bodies are stored out of line, so we need to pull them explicitly in the hash.
139+
bodies.hash_stable(&mut hcx, &mut stable_hasher);
140+
let h1 = stable_hasher.finish();
141+
142+
let mut stable_hasher = StableHasher::new();
143+
attrs.hash_stable(&mut hcx, &mut stable_hasher);
144+
let h2 = stable_hasher.finish();
145+
(Some(h1), Some(h2))
146+
})
147+
} else {
148+
(None, None)
149+
}
150+
}
124151
}
125152

126153
pub fn provide(providers: &mut Providers) {

compiler/rustc_ty_utils/src/assoc.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,24 @@ fn associated_types_for_impl_traits_in_associated_fn(
240240

241241
fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) {
242242
feed.local_def_id_to_hir_id(HirId::make_owner(feed.def_id()));
243+
244+
let node = hir::OwnerNode::AssocOpaqueTy(&hir::AssocOpaqueTy {});
245+
let bodies = Default::default();
246+
let attrs = hir::AttributeMap::EMPTY;
247+
248+
let (opt_hash_including_bodies, _) = feed.tcx.hash_owner_nodes(node, &bodies, &attrs.map);
243249
feed.opt_hir_owner_nodes(Some(feed.tcx.arena.alloc(hir::OwnerNodes {
244-
opt_hash_including_bodies: None,
250+
opt_hash_including_bodies,
245251
nodes: IndexVec::from_elem_n(
246252
hir::ParentedNode {
247253
parent: hir::ItemLocalId::INVALID,
248254
node: hir::Node::AssocOpaqueTy(&hir::AssocOpaqueTy {}),
249255
},
250256
1,
251257
),
252-
bodies: Default::default(),
258+
bodies,
253259
})));
254-
feed.feed_owner_id().hir_attrs(hir::AttributeMap::EMPTY);
260+
feed.feed_owner_id().hir_attrs(attrs);
255261
}
256262

257263
/// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Issue #122508
2+
3+
//@ check-pass
4+
//@ incremental
5+
//@ edition:2021
6+
7+
trait MyTrait {
8+
async fn bar(&self) -> i32;
9+
}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)