Skip to content

Commit 5152355

Browse files
committed
Make encode_attrs use opt_local_def_id_to_hir_id so we can feed it with None for definitions that have no HIR
1 parent 63b3bac commit 5152355

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11271127
let mut is_public: Option<bool> = None;
11281128

11291129
let mut attrs = tcx
1130-
.hir()
1131-
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
1130+
.opt_local_def_id_to_hir_id(def_id)
1131+
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
11321132
.iter()
11331133
.filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public));
11341134

compiler/rustc_middle/src/hir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ pub fn provide(providers: &mut Providers) {
120120
let node = owner.node();
121121
Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
122122
};
123-
providers.local_def_id_to_hir_id = |tcx, id| {
123+
providers.opt_local_def_id_to_hir_id = |tcx, id| {
124124
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
125-
match owner {
125+
Some(match owner {
126126
MaybeOwner::Owner(_) => HirId::make_owner(id),
127127
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
128128
MaybeOwner::NonOwner(hir_id) => hir_id,
129-
}
129+
})
130130
};
131131
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes);
132132
providers.hir_owner_parent = |tcx, id| {

compiler/rustc_middle/src/query/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ rustc_queries! {
8585
desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
8686
}
8787

88-
/// Gives access to the HIR ID for the given `LocalDefId` owner `key`.
88+
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
8989
///
90-
/// This can be conveniently accessed by methods on `tcx.hir()`.
91-
/// Avoid calling this query directly.
92-
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
90+
/// Definitions that were generated with no HIR, would be feeded to return `None`.
91+
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
9392
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
9493
}
9594

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,10 @@ impl<'tcx> TyCtxt<'tcx> {
23592359
})
23602360
)
23612361
}
2362+
2363+
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
2364+
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
2365+
}
23622366
}
23632367

23642368
impl<'tcx> TyCtxtAt<'tcx> {

0 commit comments

Comments
 (0)