diff --git a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs index 05fbbf45d7c24..19a543ae777f0 100644 --- a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs @@ -1,7 +1,8 @@ use rustc_hir as hir; +use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{DefIdTree, TyCtxt}; use rustc_span::symbol::Symbol; use rustc_target::spec::abi::Abi; @@ -16,44 +17,47 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option { } pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let parent_id = tcx.hir().get_parent_node(hir_id); - matches!( - tcx.hir().get(parent_id), - hir::Node::Item(hir::Item { - kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }), - .. - }) - ) + let parent_id = tcx.local_parent(def_id).unwrap(); + tcx.def_kind(parent_id) == DefKind::Impl + && tcx.impl_constness(parent_id) == hir::Constness::Const } /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether /// said intrinsic has a `rustc_const_{un,}stable` attribute. -fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { +fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness { let def_id = def_id.expect_local(); let node = tcx.hir().get_by_def_id(def_id); - if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) = - node - { - // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other - // foreign items cannot be evaluated at compile-time. - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) { - tcx.lookup_const_stability(def_id).is_some() - } else { - false - } - } else if let Some(fn_kind) = node.fn_kind() { - if fn_kind.constness() == hir::Constness::Const { - return true; + match node { + hir::Node::Ctor(_) => hir::Constness::Const, + hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness, + hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => { + // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other + // foreign items cannot be evaluated at compile-time. + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); + let is_const = if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = + tcx.hir().get_foreign_abi(hir_id) + { + tcx.lookup_const_stability(def_id).is_some() + } else { + false + }; + if is_const { hir::Constness::Const } else { hir::Constness::NotConst } } + _ => { + if let Some(fn_kind) = node.fn_kind() { + if fn_kind.constness() == hir::Constness::Const { + return hir::Constness::Const; + } - // If the function itself is not annotated with `const`, it may still be a `const fn` - // if it resides in a const trait impl. - is_parent_const_impl_raw(tcx, def_id) - } else { - matches!(node, hir::Node::Ctor(_)) + // If the function itself is not annotated with `const`, it may still be a `const fn` + // if it resides in a const trait impl. + let is_const = is_parent_const_impl_raw(tcx, def_id); + if is_const { hir::Constness::Const } else { hir::Constness::NotConst } + } else { + hir::Constness::NotConst + } + } } } @@ -77,5 +81,5 @@ fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool { } pub fn provide(providers: &mut Providers) { - *providers = Providers { is_const_fn_raw, is_promotable_const_fn, ..*providers }; + *providers = Providers { impl_constness, is_promotable_const_fn, ..*providers }; } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 388d36e2766fc..046322a42d85b 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -14,7 +14,6 @@ use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell}; use rustc_data_structures::unhash::UnhashMap; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive}; -use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; @@ -1419,19 +1418,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - // This replicates some of the logic of the crate-local `is_const_fn_raw` query, because we - // don't serialize constness for tuple variant and tuple struct constructors. - fn is_const_fn_raw(self, id: DefIndex) -> bool { - let constness = match self.kind(id) { - EntryKind::AssocFn(_) | EntryKind::Fn | EntryKind::ForeignFn => { - self.root.tables.impl_constness.get(self, id).unwrap().decode(self) - } - EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const, - _ => hir::Constness::NotConst, - }; - constness == hir::Constness::Const - } - fn is_foreign_item(self, id: DefIndex) -> bool { match self.kind(id) { EntryKind::ForeignStatic | EntryKind::ForeignFn => true, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 87ea041b43202..cd3a1d72d41d2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -163,7 +163,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) } associated_item => { cdata.get_associated_item(def_id.index) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } - is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } is_foreign_item => { cdata.is_foreign_item(def_id.index) } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } trait_of_item => { cdata.get_trait_of_item(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 648a7972cbb78..6c758b8e5b633 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1048,6 +1048,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); + record!(self.tables.impl_constness[def_id] <- hir::Constness::Const); record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { assert!(f.did.is_local()); f.did.index @@ -1077,6 +1078,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); + record!(self.tables.impl_constness[def_id] <- hir::Constness::Const); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); @@ -1155,6 +1157,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; record!(self.tables.repr_options[def_id] <- adt_def.repr()); + record!(self.tables.impl_constness[def_id] <- hir::Constness::Const); record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data))); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { @@ -1417,6 +1420,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::ItemKind::Struct(ref struct_def, _) => { let adt_def = self.tcx.adt_def(def_id); record!(self.tables.repr_options[def_id] <- adt_def.repr()); + record!(self.tables.impl_constness[def_id] <- hir::Constness::Const); // Encode def_ids for each field and method // for methods, write all the stuff get_trait_method diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index bf45269e06f30..14c11187fc5ee 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -559,7 +559,7 @@ rustc_queries! { /// /// **Do not call this function manually.** It is only meant to cache the base data for the /// `is_const_fn` function. - query is_const_fn_raw(key: DefId) -> bool { + query impl_constness(key: DefId) -> hir::Constness { desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) } separate_provide_extern } @@ -1329,11 +1329,6 @@ rustc_queries! { separate_provide_extern } - query impl_constness(def_id: DefId) -> hir::Constness { - desc { |tcx| "looking up whether `{}` is a const impl", tcx.def_path_str(def_id) } - separate_provide_extern - } - query check_item_well_formed(key: LocalDefId) -> () { desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 653b4908ab560..37425c91157b3 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -289,6 +289,11 @@ pub struct ClosureSizeProfileData<'tcx> { pub trait DefIdTree: Copy { fn parent(self, id: DefId) -> Option; + #[inline] + fn local_parent(self, id: LocalDefId) -> Option { + Some(self.parent(id.to_def_id())?.expect_local()) + } + fn is_descendant_of(self, mut descendant: DefId, ancestor: DefId) -> bool { if descendant.krate != ancestor.krate { return false; @@ -2256,6 +2261,12 @@ impl<'tcx> TyCtxt<'tcx> { pub fn is_object_safe(self, key: DefId) -> bool { self.object_safety_violations(key).is_empty() } + + #[inline] + pub fn is_const_fn_raw(self, def_id: DefId) -> bool { + matches!(self.def_kind(def_id), DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..)) + && self.impl_constness(def_id) == hir::Constness::Const + } } /// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition. diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 8aa659fa6ac7c..7ad8748135630 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1859,7 +1859,7 @@ impl CheckAttrVisitor<'_> { ) -> bool { match target { Target::Fn | Target::Method(_) - if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id)) => + if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id).to_def_id()) => { true } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 44ef0a09a654b..64145bbf189ff 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -77,15 +77,6 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { } } -fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness { - let item = tcx.hir().expect_item(def_id.expect_local()); - if let hir::ItemKind::Impl(impl_) = &item.kind { - impl_.constness - } else { - bug!("`impl_constness` called on {:?}", item); - } -} - /// Calculates the `Sized` constraint. /// /// In fact, there are only a few options for the types in the constraint: @@ -498,7 +489,6 @@ pub fn provide(providers: &mut ty::query::Providers) { instance_def_size_estimate, issue33140_self_ty, impl_defaultness, - impl_constness, conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw, ..*providers };