Skip to content

Commit 2e35f95

Browse files
committed
Auto merge of rust-lang#98960 - cjgillot:entry-kind, r=estebank
Remove EntryKind from metadata. This PR continues the refactor of metadata emission to be more systematic, iterating on definitions and filtering based on each definition's `DefKind`. This allows to remove the large `EntryKind` enum, replaced by linear tables in metadata.
2 parents 9af618b + 60a052f commit 2e35f95

File tree

7 files changed

+252
-355
lines changed

7 files changed

+252
-355
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+50-68
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::creader::{CStore, CrateMetadataRef};
44
use crate::rmeta::*;
55

66
use rustc_ast as ast;
7-
use rustc_ast::ptr::P;
87
use rustc_data_structures::captures::Captures;
98
use rustc_data_structures::fx::FxHashMap;
109
use rustc_data_structures::svh::Svh;
@@ -33,7 +32,7 @@ use rustc_session::cstore::{
3332
use rustc_session::Session;
3433
use rustc_span::hygiene::{ExpnIndex, MacroKind};
3534
use rustc_span::source_map::{respan, Spanned};
36-
use rustc_span::symbol::{sym, Ident, Symbol};
35+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3736
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3837

3938
use proc_macro::bridge::client::ProcMacro;
@@ -785,26 +784,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
785784
self.opt_item_ident(item_index, sess).expect("no encoded ident for item")
786785
}
787786

788-
fn maybe_kind(self, item_id: DefIndex) -> Option<EntryKind> {
789-
self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
790-
}
791-
792787
#[inline]
793788
pub(super) fn map_encoded_cnum_to_current(self, cnum: CrateNum) -> CrateNum {
794789
if cnum == LOCAL_CRATE { self.cnum } else { self.cnum_map[cnum] }
795790
}
796791

797-
fn kind(self, item_id: DefIndex) -> EntryKind {
798-
self.maybe_kind(item_id).unwrap_or_else(|| {
799-
bug!(
800-
"CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
801-
item_id,
802-
self.root.name,
803-
self.cnum,
804-
)
805-
})
806-
}
807-
808792
fn def_kind(self, item_id: DefIndex) -> DefKind {
809793
self.root.tables.opt_def_kind.get(self, item_id).unwrap_or_else(|| {
810794
bug!(
@@ -856,21 +840,16 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
856840
)
857841
}
858842

859-
fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
860-
let data = match kind {
861-
EntryKind::Variant(data) | EntryKind::Struct(data) | EntryKind::Union(data) => {
862-
data.decode(self)
863-
}
864-
_ => bug!(),
865-
};
866-
843+
fn get_variant(self, kind: &DefKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
867844
let adt_kind = match kind {
868-
EntryKind::Variant(_) => ty::AdtKind::Enum,
869-
EntryKind::Struct(..) => ty::AdtKind::Struct,
870-
EntryKind::Union(..) => ty::AdtKind::Union,
845+
DefKind::Variant => ty::AdtKind::Enum,
846+
DefKind::Struct => ty::AdtKind::Struct,
847+
DefKind::Union => ty::AdtKind::Union,
871848
_ => bug!(),
872849
};
873850

851+
let data = self.root.tables.variant_data.get(self, index).unwrap().decode(self);
852+
874853
let variant_did =
875854
if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
876855
let ctor_did = data.ctor.map(|index| self.local_def_id(index));
@@ -901,13 +880,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
901880
}
902881

903882
fn get_adt_def(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::AdtDef<'tcx> {
904-
let kind = self.kind(item_id);
883+
let kind = self.def_kind(item_id);
905884
let did = self.local_def_id(item_id);
906885

907886
let adt_kind = match kind {
908-
EntryKind::Enum => ty::AdtKind::Enum,
909-
EntryKind::Struct(_) => ty::AdtKind::Struct,
910-
EntryKind::Union(_) => ty::AdtKind::Union,
887+
DefKind::Enum => ty::AdtKind::Enum,
888+
DefKind::Struct => ty::AdtKind::Struct,
889+
DefKind::Union => ty::AdtKind::Union,
911890
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
912891
};
913892
let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode(self);
@@ -919,7 +898,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
919898
.get(self, item_id)
920899
.unwrap_or_else(LazyArray::empty)
921900
.decode(self)
922-
.map(|index| self.get_variant(&self.kind(index), index, did))
901+
.map(|index| self.get_variant(&self.def_kind(index), index, did))
923902
.collect()
924903
} else {
925904
std::iter::once(self.get_variant(&kind, item_id, did)).collect()
@@ -1029,10 +1008,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10291008
let vis = self.get_visibility(child_index);
10301009
let span = self.get_span(child_index, sess);
10311010
let macro_rules = match kind {
1032-
DefKind::Macro(..) => match self.kind(child_index) {
1033-
EntryKind::MacroDef(_, macro_rules) => macro_rules,
1034-
_ => unreachable!(),
1035-
},
1011+
DefKind::Macro(..) => {
1012+
self.root.tables.macro_rules.get(self, child_index).is_some()
1013+
}
10361014
_ => false,
10371015
};
10381016

@@ -1086,14 +1064,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10861064
}
10871065
}
10881066

1089-
match self.kind(id) {
1090-
EntryKind::Mod(exports) => {
1091-
for exp in exports.decode((self, sess)) {
1092-
callback(exp);
1093-
}
1067+
if let Some(exports) = self.root.tables.module_reexports.get(self, id) {
1068+
for exp in exports.decode((self, sess)) {
1069+
callback(exp);
10941070
}
1095-
EntryKind::Enum | EntryKind::Trait => {}
1096-
_ => bug!("`for_each_module_child` is called on a non-module: {:?}", self.def_kind(id)),
10971071
}
10981072
}
10991073

@@ -1106,19 +1080,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11061080
}
11071081

11081082
fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId {
1109-
match self.kind(id) {
1110-
EntryKind::Mod(_) | EntryKind::Enum | EntryKind::Trait => {
1111-
self.get_expn_that_defined(id, sess)
1112-
}
1083+
match self.def_kind(id) {
1084+
DefKind::Mod | DefKind::Enum | DefKind::Trait => self.get_expn_that_defined(id, sess),
11131085
_ => panic!("Expected module, found {:?}", self.local_def_id(id)),
11141086
}
11151087
}
11161088

1117-
fn get_fn_has_self_parameter(self, id: DefIndex) -> bool {
1118-
match self.kind(id) {
1119-
EntryKind::AssocFn { has_self, .. } => has_self,
1120-
_ => false,
1121-
}
1089+
fn get_fn_has_self_parameter(self, id: DefIndex, sess: &'a Session) -> bool {
1090+
self.root
1091+
.tables
1092+
.fn_arg_names
1093+
.get(self, id)
1094+
.unwrap_or_else(LazyArray::empty)
1095+
.decode((self, sess))
1096+
.nth(0)
1097+
.map_or(false, |ident| ident.name == kw::SelfLower)
11221098
}
11231099

11241100
fn get_associated_item_def_ids(
@@ -1135,15 +1111,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11351111
.map(move |child_index| self.local_def_id(child_index))
11361112
}
11371113

1138-
fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
1114+
fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem {
11391115
let name = self.item_name(id);
11401116

1141-
let (kind, container, has_self) = match self.kind(id) {
1142-
EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
1143-
EntryKind::AssocFn { container, has_self } => (ty::AssocKind::Fn, container, has_self),
1144-
EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
1145-
_ => bug!("cannot get associated-item of `{:?}`", id),
1117+
let kind = match self.def_kind(id) {
1118+
DefKind::AssocConst => ty::AssocKind::Const,
1119+
DefKind::AssocFn => ty::AssocKind::Fn,
1120+
DefKind::AssocTy => ty::AssocKind::Type,
1121+
_ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
11461122
};
1123+
let has_self = self.get_fn_has_self_parameter(id, sess);
1124+
let container = self.root.tables.assoc_container.get(self, id).unwrap();
11471125

11481126
ty::AssocItem {
11491127
name,
@@ -1156,9 +1134,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11561134
}
11571135

11581136
fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
1159-
match self.kind(node_id) {
1160-
EntryKind::Struct(data) | EntryKind::Variant(data) => {
1161-
let vdata = data.decode(self);
1137+
match self.def_kind(node_id) {
1138+
DefKind::Struct | DefKind::Variant => {
1139+
let vdata = self.root.tables.variant_data.get(self, node_id).unwrap().decode(self);
11621140
vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
11631141
}
11641142
_ => None,
@@ -1346,18 +1324,22 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13461324
}
13471325

13481326
fn get_macro(self, id: DefIndex, sess: &Session) -> ast::MacroDef {
1349-
match self.kind(id) {
1350-
EntryKind::MacroDef(mac_args, macro_rules) => {
1351-
ast::MacroDef { body: P(mac_args.decode((self, sess))), macro_rules }
1327+
match self.def_kind(id) {
1328+
DefKind::Macro(_) => {
1329+
let macro_rules = self.root.tables.macro_rules.get(self, id).is_some();
1330+
let body =
1331+
self.root.tables.macro_definition.get(self, id).unwrap().decode((self, sess));
1332+
ast::MacroDef { macro_rules, body: ast::ptr::P(body) }
13521333
}
13531334
_ => bug!(),
13541335
}
13551336
}
13561337

13571338
fn is_foreign_item(self, id: DefIndex) -> bool {
1358-
match self.kind(id) {
1359-
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,
1360-
_ => false,
1339+
if let Some(parent) = self.def_key(id).parent {
1340+
matches!(self.def_kind(parent), DefKind::ForeignMod)
1341+
} else {
1342+
false
13611343
}
13621344
}
13631345

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ provide! { tcx, def_id, other, cdata,
233233
associated_item_def_ids => {
234234
tcx.arena.alloc_from_iter(cdata.get_associated_item_def_ids(def_id.index, tcx.sess))
235235
}
236-
associated_item => { cdata.get_associated_item(def_id.index) }
236+
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
237237
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
238238
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
239239
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
@@ -535,8 +535,8 @@ impl CStore {
535535
)
536536
}
537537

538-
pub fn fn_has_self_parameter_untracked(&self, def: DefId) -> bool {
539-
self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index)
538+
pub fn fn_has_self_parameter_untracked(&self, def: DefId, sess: &Session) -> bool {
539+
self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index, sess)
540540
}
541541

542542
pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc<CrateSource> {

0 commit comments

Comments
 (0)