Skip to content

Commit 15b2d1a

Browse files
committed
Merge impl_constness and is_const_fn_raw.
1 parent e62f483 commit 15b2d1a

File tree

8 files changed

+52
-63
lines changed

8 files changed

+52
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc_hir as hir;
2+
use rustc_hir::def::DefKind;
23
use rustc_hir::def_id::{DefId, LocalDefId};
34
use rustc_middle::ty::query::Providers;
4-
use rustc_middle::ty::TyCtxt;
5+
use rustc_middle::ty::{DefIdTree, TyCtxt};
56
use rustc_span::symbol::Symbol;
67
use rustc_target::spec::abi::Abi;
78

@@ -16,44 +17,47 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
1617
}
1718

1819
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
19-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
20-
let parent_id = tcx.hir().get_parent_node(hir_id);
21-
matches!(
22-
tcx.hir().get(parent_id),
23-
hir::Node::Item(hir::Item {
24-
kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
25-
..
26-
})
27-
)
20+
let parent_id = tcx.local_parent(def_id).unwrap();
21+
tcx.def_kind(parent_id) == DefKind::Impl
22+
&& tcx.impl_constness(parent_id) == hir::Constness::Const
2823
}
2924

3025
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
3126
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
32-
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
27+
fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
3328
let def_id = def_id.expect_local();
3429
let node = tcx.hir().get_by_def_id(def_id);
3530

36-
if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
37-
node
38-
{
39-
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
40-
// foreign items cannot be evaluated at compile-time.
41-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
42-
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
43-
tcx.lookup_const_stability(def_id).is_some()
44-
} else {
45-
false
46-
}
47-
} else if let Some(fn_kind) = node.fn_kind() {
48-
if fn_kind.constness() == hir::Constness::Const {
49-
return true;
31+
match node {
32+
hir::Node::Ctor(_) => hir::Constness::Const,
33+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
34+
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
35+
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
36+
// foreign items cannot be evaluated at compile-time.
37+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
38+
let is_const = if let Abi::RustIntrinsic | Abi::PlatformIntrinsic =
39+
tcx.hir().get_foreign_abi(hir_id)
40+
{
41+
tcx.lookup_const_stability(def_id).is_some()
42+
} else {
43+
false
44+
};
45+
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
5046
}
47+
_ => {
48+
if let Some(fn_kind) = node.fn_kind() {
49+
if fn_kind.constness() == hir::Constness::Const {
50+
return hir::Constness::Const;
51+
}
5152

52-
// If the function itself is not annotated with `const`, it may still be a `const fn`
53-
// if it resides in a const trait impl.
54-
is_parent_const_impl_raw(tcx, def_id)
55-
} else {
56-
matches!(node, hir::Node::Ctor(_))
53+
// If the function itself is not annotated with `const`, it may still be a `const fn`
54+
// if it resides in a const trait impl.
55+
let is_const = is_parent_const_impl_raw(tcx, def_id);
56+
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
57+
} else {
58+
hir::Constness::NotConst
59+
}
60+
}
5761
}
5862
}
5963

@@ -77,5 +81,5 @@ fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
7781
}
7882

7983
pub fn provide(providers: &mut Providers) {
80-
*providers = Providers { is_const_fn_raw, is_promotable_const_fn, ..*providers };
84+
*providers = Providers { impl_constness, is_promotable_const_fn, ..*providers };
8185
}

compiler/rustc_metadata/src/rmeta/decoder.rs

-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
1414
use rustc_data_structures::unhash::UnhashMap;
1515
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1616
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
17-
use rustc_hir as hir;
1817
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1918
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2019
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
@@ -1419,19 +1418,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14191418
}
14201419
}
14211420

1422-
// This replicates some of the logic of the crate-local `is_const_fn_raw` query, because we
1423-
// don't serialize constness for tuple variant and tuple struct constructors.
1424-
fn is_const_fn_raw(self, id: DefIndex) -> bool {
1425-
let constness = match self.kind(id) {
1426-
EntryKind::AssocFn(_) | EntryKind::Fn | EntryKind::ForeignFn => {
1427-
self.root.tables.impl_constness.get(self, id).unwrap().decode(self)
1428-
}
1429-
EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1430-
_ => hir::Constness::NotConst,
1431-
};
1432-
constness == hir::Constness::Const
1433-
}
1434-
14351421
fn is_foreign_item(self, id: DefIndex) -> bool {
14361422
match self.kind(id) {
14371423
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
163163
associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) }
164164
associated_item => { cdata.get_associated_item(def_id.index) }
165165
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
166-
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
167166
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
168167
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
169168
trait_of_item => { cdata.get_trait_of_item(def_id.index) }

compiler/rustc_metadata/src/rmeta/encoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10481048
};
10491049

10501050
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
1051+
record!(self.tables.impl_constness[def_id] <- hir::Constness::Const);
10511052
record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
10521053
assert!(f.did.is_local());
10531054
f.did.index
@@ -1077,6 +1078,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10771078
};
10781079

10791080
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
1081+
record!(self.tables.impl_constness[def_id] <- hir::Constness::Const);
10801082
self.encode_item_type(def_id);
10811083
if variant.ctor_kind == CtorKind::Fn {
10821084
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
@@ -1155,6 +1157,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11551157
};
11561158

11571159
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1160+
record!(self.tables.impl_constness[def_id] <- hir::Constness::Const);
11581161
record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data)));
11591162
self.encode_item_type(def_id);
11601163
if variant.ctor_kind == CtorKind::Fn {
@@ -1417,6 +1420,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14171420
hir::ItemKind::Struct(ref struct_def, _) => {
14181421
let adt_def = self.tcx.adt_def(def_id);
14191422
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1423+
record!(self.tables.impl_constness[def_id] <- hir::Constness::Const);
14201424

14211425
// Encode def_ids for each field and method
14221426
// for methods, write all the stuff get_trait_method

compiler/rustc_middle/src/query/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ rustc_queries! {
559559
///
560560
/// **Do not call this function manually.** It is only meant to cache the base data for the
561561
/// `is_const_fn` function.
562-
query is_const_fn_raw(key: DefId) -> bool {
562+
query impl_constness(key: DefId) -> hir::Constness {
563563
desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) }
564564
separate_provide_extern
565565
}
@@ -1329,11 +1329,6 @@ rustc_queries! {
13291329
separate_provide_extern
13301330
}
13311331

1332-
query impl_constness(def_id: DefId) -> hir::Constness {
1333-
desc { |tcx| "looking up whether `{}` is a const impl", tcx.def_path_str(def_id) }
1334-
separate_provide_extern
1335-
}
1336-
13371332
query check_item_well_formed(key: LocalDefId) -> () {
13381333
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
13391334
}

compiler/rustc_middle/src/ty/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ pub struct ClosureSizeProfileData<'tcx> {
289289
pub trait DefIdTree: Copy {
290290
fn parent(self, id: DefId) -> Option<DefId>;
291291

292+
#[inline]
293+
fn local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
294+
Some(self.parent(id.to_def_id())?.expect_local())
295+
}
296+
292297
fn is_descendant_of(self, mut descendant: DefId, ancestor: DefId) -> bool {
293298
if descendant.krate != ancestor.krate {
294299
return false;
@@ -2256,6 +2261,12 @@ impl<'tcx> TyCtxt<'tcx> {
22562261
pub fn is_object_safe(self, key: DefId) -> bool {
22572262
self.object_safety_violations(key).is_empty()
22582263
}
2264+
2265+
#[inline]
2266+
pub fn is_const_fn_raw(self, def_id: DefId) -> bool {
2267+
matches!(self.def_kind(def_id), DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..))
2268+
&& self.impl_constness(def_id) == hir::Constness::Const
2269+
}
22592270
}
22602271

22612272
/// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition.

compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ impl CheckAttrVisitor<'_> {
18591859
) -> bool {
18601860
match target {
18611861
Target::Fn | Target::Method(_)
1862-
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id)) =>
1862+
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id).to_def_id()) =>
18631863
{
18641864
true
18651865
}

compiler/rustc_ty_utils/src/ty.rs

-10
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,6 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
7777
}
7878
}
7979

80-
fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
81-
let item = tcx.hir().expect_item(def_id.expect_local());
82-
if let hir::ItemKind::Impl(impl_) = &item.kind {
83-
impl_.constness
84-
} else {
85-
bug!("`impl_constness` called on {:?}", item);
86-
}
87-
}
88-
8980
/// Calculates the `Sized` constraint.
9081
///
9182
/// 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) {
498489
instance_def_size_estimate,
499490
issue33140_self_ty,
500491
impl_defaultness,
501-
impl_constness,
502492
conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw,
503493
..*providers
504494
};

0 commit comments

Comments
 (0)