diff options
| author | Michael Goulet <michael@errs.io> | 2023-03-13 18:54:05 +0000 | 
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2023-03-21 15:38:51 +0000 | 
| commit | 2eb1c08e433aadf2362a65f2ef1387670a6d34cf (patch) | |
| tree | 1d8465f4d80adbbe4a44dd72eedc3361bd2b8f7c /compiler/rustc_middle | |
| parent | a01b4cc9f375f1b95fa8195daeea938d3d9c4c34 (diff) | |
| download | rust-2eb1c08e433aadf2362a65f2ef1387670a6d34cf.tar.gz rust-2eb1c08e433aadf2362a65f2ef1387670a6d34cf.zip | |
Use local key in providers
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/hir/map/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/hir/mod.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/query/keys.rs | 311 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/consts.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 15 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/instance.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/query.rs | 31 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 7 | 
9 files changed, 262 insertions, 147 deletions
| diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 8a4c10cd71c..d877a2c5494 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -6,7 +6,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::*; @@ -1131,8 +1131,7 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> { } } -pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { - debug_assert_eq!(crate_num, LOCAL_CRATE); +pub(super) fn crate_hash(tcx: TyCtxt<'_>, (): ()) -> Svh { let krate = tcx.hir_crate(()); let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash"); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 403b2b65088..0d8a8c9cdfd 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -147,18 +147,18 @@ pub fn provide(providers: &mut Providers) { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; providers.def_span = |tcx, def_id| { - let def_id = def_id.expect_local(); + let def_id = def_id; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP) }; providers.def_ident_span = |tcx, def_id| { - let def_id = def_id.expect_local(); + let def_id = def_id; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx.hir().opt_ident_span(hir_id) }; providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); - let def_id = id.expect_local(); + let def_id = id; let hir_id = hir.local_def_id_to_hir_id(def_id); if let Some(body_id) = hir.maybe_body_owned_by(def_id) { tcx.arena.alloc_from_iter(hir.body_param_names(body_id)) @@ -176,12 +176,10 @@ pub fn provide(providers: &mut Providers) { span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id); } }; - providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local()); + providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id); providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls; - providers.expn_that_defined = |tcx, id| { - let id = id.expect_local(); - tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root()) - }; + providers.expn_that_defined = + |tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root()); providers.in_scope_traits_map = |tcx, id| { tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map) }; diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 6e961a775c1..584e94bf847 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -26,9 +26,11 @@ pub trait Key: Sized { // r-a issue: <https://github.com/rust-lang/rust-analyzer/issues/13693> type CacheSelector; + type LocalKey; + /// Given an instance of this key, what crate is it referring to? /// This is used to find the provider. - fn query_crate_is_local(&self) -> bool; + fn as_local_key(&self) -> Option<Self::LocalKey>; /// In the event that a cycle occurs, if no explicit span has been /// given for a query with key `self`, what span should we use? @@ -47,10 +49,11 @@ pub trait Key: Sized { impl Key for () { type CacheSelector = SingleCacheSelector; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -60,10 +63,11 @@ impl Key for () { impl<'tcx> Key for ty::InstanceDef<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().is_local() + fn as_local_key(&self) -> Option<Self> { + self.def_id().is_local().then(|| *self) } fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -73,10 +77,11 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> { impl<'tcx> Key for ty::Instance<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().is_local() + fn as_local_key(&self) -> Option<Self> { + self.def_id().is_local().then(|| *self) } fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -86,10 +91,11 @@ impl<'tcx> Key for ty::Instance<'tcx> { impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -99,10 +105,11 @@ impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -112,10 +119,11 @@ impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) { impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { @@ -125,11 +133,13 @@ impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> { impl Key for CrateNum { type CacheSelector = VecCacheSelector<Self>; + type LocalKey = (); #[inline(always)] - fn query_crate_is_local(&self) -> bool { - *self == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + (*self == LOCAL_CRATE).then_some(()) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -137,14 +147,17 @@ impl Key for CrateNum { impl Key for OwnerId { type CacheSelector = VecCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.to_def_id().default_span(tcx) } + fn key_as_def_id(&self) -> Option<DefId> { Some(self.to_def_id()) } @@ -152,14 +165,17 @@ impl Key for OwnerId { impl Key for LocalDefId { type CacheSelector = VecCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.to_def_id().default_span(tcx) } + fn key_as_def_id(&self) -> Option<DefId> { Some(self.to_def_id()) } @@ -167,14 +183,17 @@ impl Key for LocalDefId { impl Key for DefId { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = LocalDefId; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.as_local() } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(*self) } + #[inline(always)] fn key_as_def_id(&self) -> Option<DefId> { Some(*self) @@ -183,11 +202,13 @@ impl Key for DefId { impl Key for ty::WithOptConstParam<LocalDefId> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.did.default_span(tcx) } @@ -195,11 +216,13 @@ impl Key for ty::WithOptConstParam<LocalDefId> { impl Key for SimplifiedType { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -207,11 +230,13 @@ impl Key for SimplifiedType { impl Key for (DefId, DefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, DefId); #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } @@ -219,11 +244,13 @@ impl Key for (DefId, DefId) { impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -231,11 +258,13 @@ impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { impl Key for (DefId, LocalDefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, LocalDefId); #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } @@ -243,11 +272,13 @@ impl Key for (DefId, LocalDefId) { impl Key for (LocalDefId, DefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -255,11 +286,13 @@ impl Key for (LocalDefId, DefId) { impl Key for (LocalDefId, LocalDefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -267,14 +300,16 @@ impl Key for (LocalDefId, LocalDefId) { impl Key for (DefId, Option<Ident>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, Option<Ident>); - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.0) } + #[inline(always)] fn key_as_def_id(&self) -> Option<DefId> { Some(self.0) @@ -283,11 +318,12 @@ impl Key for (DefId, Option<Ident>) { impl Key for (DefId, LocalDefId, Ident) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, LocalDefId, Ident); - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1, self.2)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } @@ -295,11 +331,13 @@ impl Key for (DefId, LocalDefId, Ident) { impl Key for (CrateNum, DefId) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = DefId; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0 == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + (self.0 == LOCAL_CRATE).then_some(self.1) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.1.default_span(tcx) } @@ -307,11 +345,13 @@ impl Key for (CrateNum, DefId) { impl Key for (CrateNum, SimplifiedType) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = SimplifiedType; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0 == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + (self.0 == LOCAL_CRATE).then_some(self.1) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -319,11 +359,13 @@ impl Key for (CrateNum, SimplifiedType) { impl Key for (DefId, SimplifiedType) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, SimplifiedType); #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -331,11 +373,13 @@ impl Key for (DefId, SimplifiedType) { impl<'tcx> Key for SubstsRef<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -343,11 +387,13 @@ impl<'tcx> Key for SubstsRef<'tcx> { impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = (LocalDefId, SubstsRef<'tcx>); #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + Some((self.0.as_local()?, self.1)) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -355,11 +401,13 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - (self.0).def.did.krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.0.def.is_local().then_some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { (self.0).def.did.default_span(tcx) } @@ -367,11 +415,13 @@ impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.0.default_span(tcx) } @@ -379,11 +429,13 @@ impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.1.def_id().krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.1.def_id().is_local().then_some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.1.def_id()) } @@ -391,11 +443,13 @@ impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) { impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -403,11 +457,13 @@ impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) { impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -415,11 +471,13 @@ impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> { impl<'tcx> Key for ty::PolyTraitRef<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.def_id().is_local().then_some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } @@ -427,11 +485,13 @@ impl<'tcx> Key for ty::PolyTraitRef<'tcx> { impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.def_id().krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.def_id().is_local().then_some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.def_id()) } @@ -439,11 +499,13 @@ impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.0.def_id().krate == LOCAL_CRATE + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.0.def_id().is_local().then_some(*self) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.def_span(self.0.def_id()) } @@ -451,11 +513,13 @@ impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { impl<'tcx> Key for GenericArg<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -463,11 +527,13 @@ impl<'tcx> Key for GenericArg<'tcx> { impl<'tcx> Key for mir::ConstantKind<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -475,11 +541,13 @@ impl<'tcx> Key for mir::ConstantKind<'tcx> { impl<'tcx> Key for ty::Const<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -487,14 +555,17 @@ impl<'tcx> Key for ty::Const<'tcx> { impl<'tcx> Key for Ty<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } + fn ty_adt_id(&self) -> Option<DefId> { match self.kind() { ty::Adt(adt, _) => Some(adt.did()), @@ -505,11 +576,13 @@ impl<'tcx> Key for Ty<'tcx> { impl<'tcx> Key for TyAndLayout<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -517,11 +590,13 @@ impl<'tcx> Key for TyAndLayout<'tcx> { impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -529,11 +604,13 @@ impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -541,11 +618,13 @@ impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> { impl<'tcx> Key for ty::ParamEnv<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -553,11 +632,12 @@ impl<'tcx> Key for ty::ParamEnv<'tcx> { impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = ty::ParamEnvAnd<'tcx, T::LocalKey>; - #[inline(always)] - fn query_crate_is_local(&self) -> bool { - self.value.query_crate_is_local() + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.value.as_local_key().map(|value| ty::ParamEnvAnd { param_env: self.param_env, value }) } + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { self.value.default_span(tcx) } @@ -565,11 +645,13 @@ impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> { impl Key for Symbol { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -577,11 +659,13 @@ impl Key for Symbol { impl Key for Option<Symbol> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } + fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { DUMMY_SP } @@ -589,12 +673,13 @@ impl Key for Option<Symbol> { /// Canonical query goals correspond to abstract trait operations that /// are not tied to any crate in particular. -impl<'tcx, T> Key for Canonical<'tcx, T> { +impl<'tcx, T: Clone> Key for Canonical<'tcx, T> { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(self.clone()) } fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { @@ -604,10 +689,11 @@ impl<'tcx, T> Key for Canonical<'tcx, T> { impl Key for (Symbol, u32, u32) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { @@ -617,10 +703,11 @@ impl Key for (Symbol, u32, u32) { impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { @@ -630,10 +717,11 @@ impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { @@ -643,10 +731,11 @@ impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -656,10 +745,11 @@ impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) { impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -669,10 +759,11 @@ impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) { impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -682,10 +773,11 @@ impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) { impl Key for HirId { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -700,11 +792,12 @@ impl Key for HirId { impl<'tcx> Key for (ValidityRequirement, ty::ParamEnvAnd<'tcx, Ty<'tcx>>) { type CacheSelector = DefaultCacheSelector<Self>; + type LocalKey = Self; // Just forward to `Ty<'tcx>` #[inline(always)] - fn query_crate_is_local(&self) -> bool { - true + fn as_local_key(&self) -> Option<Self> { + Some(*self) } fn default_span(&self, _: TyCtxt<'_>) -> Span { diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 42101f6b931..712c1a31a52 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -4,7 +4,7 @@ use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; use rustc_data_structures::intern::Interned; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::LocalDefId; use rustc_macros::HashStable; use std::fmt; @@ -265,8 +265,8 @@ impl<'tcx> Const<'tcx> { } } -pub fn const_param_default(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Const<'_>> { - let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) { +pub fn const_param_default(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Const<'_>> { + let default_def_id = match tcx.hir().get_by_def_id(def_id) { hir::Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { default: Some(ac), .. }, .. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e4694809cd1..23ac99d3397 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2518,16 +2518,11 @@ pub fn provide(providers: &mut ty::query::Providers) { providers.extern_mod_stmt_cnum = |tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned(); - providers.is_panic_runtime = |tcx, cnum| { - assert_eq!(cnum, LOCAL_CRATE); - tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::panic_runtime) - }; - providers.is_compiler_builtins = |tcx, cnum| { - assert_eq!(cnum, LOCAL_CRATE); - tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins) - }; - providers.has_panic_handler = |tcx, cnum| { - assert_eq!(cnum, LOCAL_CRATE); + providers.is_panic_runtime = + |tcx, ()| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::panic_runtime); + providers.is_compiler_builtins = + |tcx, ()| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins); + providers.has_panic_handler = |tcx, ()| { // We want to check if the panic handler was defined in this crate tcx.lang_items().panic_impl().map_or(false, |did| did.is_local()) }; diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 5fc98f01a54..aa10a651c07 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -187,7 +187,11 @@ impl<'tcx> InstanceDef<'tcx> { } #[inline] - pub fn get_attrs(&self, tcx: TyCtxt<'tcx>, attr: Symbol) -> ty::Attributes<'tcx> { + pub fn get_attrs( + &self, + tcx: TyCtxt<'tcx>, + attr: Symbol, + ) -> impl Iterator<Item = &'tcx rustc_ast::Attribute> { tcx.get_attrs(self.def_id(), attr) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 298b2c3073c..d8da8e0f34a 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2027,7 +2027,6 @@ impl<'tcx> FieldDef { } } -pub type Attributes<'tcx> = impl Iterator<Item = &'tcx ast::Attribute>; #[derive(Debug, PartialEq, Eq)] pub enum ImplOverlapKind { /// These impls are always allowed to overlap. @@ -2375,7 +2374,12 @@ impl<'tcx> TyCtxt<'tcx> { } /// Gets all attributes with the given name. - pub fn get_attrs(self, did: DefId, attr: Symbol) -> ty::Attributes<'tcx> { + pub fn get_attrs( + self, + did: impl Into<DefId>, + attr: Symbol, + ) -> impl Iterator<Item = &'tcx ast::Attribute> { + let did: DefId = did.into(); let filter_fn = move |a: &&ast::Attribute| a.has_name(attr); if let Some(did) = did.as_local() { self.hir().attrs(self.hir().local_def_id_to_hir_id(did)).iter().filter(filter_fn) @@ -2386,8 +2390,9 @@ impl<'tcx> TyCtxt<'tcx> { } } - pub fn get_attr(self, did: DefId, attr: Symbol) -> Option<&'tcx ast::Attribute> { + pub fn get_attr(self, did: impl Into<DefId>, attr: Symbol) -> Option<&'tcx ast::Attribute> { if cfg!(debug_assertions) && !rustc_feature::is_valid_for_get_attr(attr) { + let did: DefId = did.into(); bug!("get_attr: unexpected called with DefId `{:?}`, attr `{:?}`", did, attr); } else { self.get_attrs(did, attr).next() @@ -2395,7 +2400,8 @@ impl<'tcx> TyCtxt<'tcx> { } /// Determines whether an item is annotated with an attribute. - pub fn has_attr(self, did: DefId, attr: Symbol) -> bool { + pub fn has_attr(self, did: impl Into<DefId>, attr: Symbol) -> bool { + let did: DefId = did.into(); if cfg!(debug_assertions) && !did.is_local() && rustc_feature::is_builtin_only_local(attr) { bug!("tried to access the `only_local` attribute `{}` from an extern crate", attr); } else { diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 05219efe5f5..3cf28408f69 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -151,6 +151,24 @@ macro_rules! query_if_arena { }; } +macro_rules! separate_provide_local_decl { + ([][$name:ident]) => { + for<'tcx> fn( + TyCtxt<'tcx>, + query_keys::$name<'tcx>, + ) -> query_provided::$name<'tcx> + }; + ([(separate_provide_extern) $($rest:tt)*][$name:ident]) => { + for<'tcx> fn( + TyCtxt<'tcx>, + query_keys_local::$name<'tcx>, + ) -> query_provided::$name<'tcx> + }; + ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => { + separate_provide_local_decl!([$($modifiers)*][$($args)*]) + }; +} + macro_rules! separate_provide_extern_decl { ([][$name:ident]) => { () @@ -212,6 +230,12 @@ macro_rules! define_callbacks { $(pub type $name<'tcx> = $($K)*;)* } #[allow(nonstandard_style, unused_lifetimes)] + pub mod query_keys_local { + use super::*; + + $(pub type $name<'tcx> = <$($K)* as Key>::LocalKey;)* + } + #[allow(nonstandard_style, unused_lifetimes)] pub mod query_values { use super::*; @@ -383,10 +407,7 @@ macro_rules! define_callbacks { } pub struct Providers { - $(pub $name: for<'tcx> fn( - TyCtxt<'tcx>, - query_keys::$name<'tcx>, - ) -> query_provided::$name<'tcx>,)* + $(pub $name: separate_provide_local_decl!([$($modifiers)*][$name]),)* } pub struct ExternProviders { @@ -405,7 +426,7 @@ macro_rules! define_callbacks { If that's not the case, {} was likely never assigned to a provider function.\n", stringify!($name), key, - if key.query_crate_is_local() { "local" } else { "external" }, + if key.as_local_key().is_some() { "local" } else { "external" }, stringify!($name), ),)* } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index dc585f438f4..dcd9743196e 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -15,7 +15,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::{CtorOf, DefKind, Res}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_index::bit_set::GrowableBitSet; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; @@ -1439,8 +1439,7 @@ pub fn reveal_opaque_types_in_bounds<'tcx>( } /// Determines whether an item is annotated with `doc(hidden)`. -fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - assert!(def_id.is_local()); +fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { tcx.get_attrs(def_id, sym::doc) .filter_map(|attr| attr.meta_item_list()) .any(|items| items.iter().any(|item| item.has_name(sym::hidden))) @@ -1454,7 +1453,7 @@ pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool { } /// Determines whether an item is an intrinsic by Abi. -pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool { +pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic) } | 
