Skip to content

Commit eb82fac

Browse files
committed
Auto merge of #94883 - cjgillot:flat-metadata, r=oli-obk
Encode even more metadata through tables instead of EntryKind This should move us closer to getting rid of `EntryKind`.
2 parents 297a801 + 15b2d1a commit eb82fac

File tree

11 files changed

+131
-212
lines changed

11 files changed

+131
-212
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

+11-55
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};
@@ -909,40 +908,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
909908
)
910909
}
911910

912-
fn get_trait_def(self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
913-
match self.kind(item_id) {
914-
EntryKind::Trait(data) => {
915-
let data = data.decode((self, sess));
916-
ty::TraitDef::new(
917-
self.local_def_id(item_id),
918-
data.unsafety,
919-
data.paren_sugar,
920-
data.has_auto_impl,
921-
data.is_marker,
922-
data.skip_array_during_method_dispatch,
923-
data.specialization_kind,
924-
self.def_path_hash(item_id),
925-
data.must_implement_one_of,
926-
)
927-
}
928-
EntryKind::TraitAlias => ty::TraitDef::new(
929-
self.local_def_id(item_id),
930-
hir::Unsafety::Normal,
931-
false,
932-
false,
933-
false,
934-
false,
935-
ty::trait_def::TraitSpecializationKind::None,
936-
self.def_path_hash(item_id),
937-
None,
938-
),
939-
_ => bug!("def-index does not refer to trait or trait alias"),
940-
}
941-
}
942-
943911
fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
944912
let data = match kind {
945-
EntryKind::Variant(data) | EntryKind::Struct(data, _) | EntryKind::Union(data, _) => {
913+
EntryKind::Variant(data) | EntryKind::Struct(data) | EntryKind::Union(data) => {
946914
data.decode(self)
947915
}
948916
_ => bug!(),
@@ -988,12 +956,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
988956
let kind = self.kind(item_id);
989957
let did = self.local_def_id(item_id);
990958

991-
let (adt_kind, repr) = match kind {
992-
EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
993-
EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
994-
EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
959+
let adt_kind = match kind {
960+
EntryKind::Enum => ty::AdtKind::Enum,
961+
EntryKind::Struct(_) => ty::AdtKind::Struct,
962+
EntryKind::Union(_) => ty::AdtKind::Union,
995963
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
996964
};
965+
let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode(self);
997966

998967
let variants = if let ty::AdtKind::Enum = adt_kind {
999968
self.root
@@ -1171,7 +1140,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11711140
callback(exp);
11721141
}
11731142
}
1174-
EntryKind::Enum(..) | EntryKind::Trait(..) => {}
1143+
EntryKind::Enum | EntryKind::Trait => {}
11751144
_ => bug!("`for_each_module_child` is called on a non-module: {:?}", self.def_kind(id)),
11761145
}
11771146
}
@@ -1186,7 +1155,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11861155

11871156
fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId {
11881157
match self.kind(id) {
1189-
EntryKind::Mod(_) | EntryKind::Enum(_) | EntryKind::Trait(_) => {
1158+
EntryKind::Mod(_) | EntryKind::Enum | EntryKind::Trait => {
11901159
self.get_expn_that_defined(id, sess)
11911160
}
11921161
_ => panic!("Expected module, found {:?}", self.local_def_id(id)),
@@ -1239,7 +1208,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12391208

12401209
fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
12411210
match self.kind(node_id) {
1242-
EntryKind::Struct(data, _) | EntryKind::Variant(data) => {
1211+
EntryKind::Struct(data) | EntryKind::Variant(data) => {
12431212
let vdata = data.decode(self);
12441213
vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
12451214
}
@@ -1395,7 +1364,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13951364
_ => return None,
13961365
}
13971366
def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
1398-
EntryKind::Trait(_) | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
1367+
EntryKind::Trait | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
13991368
_ => None,
14001369
})
14011370
}
@@ -1449,22 +1418,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14491418
}
14501419
}
14511420

1452-
// This replicates some of the logic of the crate-local `is_const_fn_raw` query, because we
1453-
// don't serialize constness for tuple variant and tuple struct constructors.
1454-
fn is_const_fn_raw(self, id: DefIndex) -> bool {
1455-
let constness = match self.kind(id) {
1456-
EntryKind::AssocFn(data) => data.decode(self).fn_data.constness,
1457-
EntryKind::Fn(data) => data.decode(self).constness,
1458-
EntryKind::ForeignFn(data) => data.decode(self).constness,
1459-
EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
1460-
_ => hir::Constness::NotConst,
1461-
};
1462-
constness == hir::Constness::Const
1463-
}
1464-
14651421
fn is_foreign_item(self, id: DefIndex) -> bool {
14661422
match self.kind(id) {
1467-
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
1423+
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,
14681424
_ => false,
14691425
}
14701426
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
153153
asyncness => { table }
154154
fn_arg_names => { table }
155155
generator_kind => { table }
156+
trait_def => { table }
156157

157-
trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) }
158158
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
159159
adt_destructor => {
160160
let _ = cdata;
@@ -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) }

0 commit comments

Comments
 (0)