Merge impl_constness and is_const_fn_raw.

This commit is contained in:
Camille GILLOT 2022-03-13 11:12:50 +01:00
parent e62f483842
commit 15b2d1a97c
8 changed files with 52 additions and 63 deletions

View file

@ -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<Symbol> {
}
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 };
}

View file

@ -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,

View file

@ -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) }

View file

@ -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

View file

@ -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()) }
}

View file

@ -289,6 +289,11 @@ pub struct ClosureSizeProfileData<'tcx> {
pub trait DefIdTree: Copy {
fn parent(self, id: DefId) -> Option<DefId>;
#[inline]
fn local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
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.

View file

@ -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
}

View file

@ -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
};