From 5152355fe3557ad390b474451ca8fce5703b19a6 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 15 Dec 2022 13:20:27 -0300 Subject: [PATCH] Make encode_attrs use opt_local_def_id_to_hir_id so we can feed it with None for definitions that have no HIR --- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 ++-- compiler/rustc_middle/src/hir/mod.rs | 6 +++--- compiler/rustc_middle/src/query/mod.rs | 7 +++---- compiler/rustc_middle/src/ty/context.rs | 4 ++++ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 856f5bc4645fd..2b5423c96ece2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1127,8 +1127,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let mut is_public: Option = None; let mut attrs = tcx - .hir() - .attrs(tcx.hir().local_def_id_to_hir_id(def_id)) + .opt_local_def_id_to_hir_id(def_id) + .map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id)) .iter() .filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public)); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 3f6e29ad611c9..bd8c3be13c29b 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -120,13 +120,13 @@ pub fn provide(providers: &mut Providers) { let node = owner.node(); Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies }) }; - providers.local_def_id_to_hir_id = |tcx, id| { + providers.opt_local_def_id_to_hir_id = |tcx, id| { let owner = tcx.hir_crate(()).owners[id].map(|_| ()); - match owner { + Some(match owner { MaybeOwner::Owner(_) => HirId::make_owner(id), MaybeOwner::Phantom => bug!("No HirId for {:?}", id), MaybeOwner::NonOwner(hir_id) => hir_id, - } + }) }; providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes); providers.hir_owner_parent = |tcx, id| { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index ab512804330b9..5177cefdf8b59 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -85,11 +85,10 @@ rustc_queries! { desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) } } - /// Gives access to the HIR ID for the given `LocalDefId` owner `key`. + /// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any. /// - /// This can be conveniently accessed by methods on `tcx.hir()`. - /// Avoid calling this query directly. - query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId { + /// Definitions that were generated with no HIR, would be feeded to return `None`. + query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option{ desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 173c5ed4feef0..b797d4e0ef6fa 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2359,6 +2359,10 @@ impl<'tcx> TyCtxt<'tcx> { }) ) } + + pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId { + self.opt_local_def_id_to_hir_id(local_def_id).unwrap() + } } impl<'tcx> TyCtxtAt<'tcx> {