Skip to content

Commit 2e35f95

Browse files
committedSep 1, 2022
Auto merge of #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> {

‎compiler/rustc_metadata/src/rmeta/encoder.rs

+174-247
Large diffs are not rendered by default.

‎compiler/rustc_metadata/src/rmeta/mod.rs

+7-35
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ macro_rules! define_tables {
334334
}
335335

336336
define_tables! {
337-
kind: Table<DefIndex, LazyValue<EntryKind>>,
338337
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
339338
children: Table<DefIndex, LazyArray<DefIndex>>,
340339

@@ -393,39 +392,13 @@ define_tables! {
393392
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
394393
generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
395394
may_have_doc_links: Table<DefIndex, ()>,
396-
}
397-
398-
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
399-
enum EntryKind {
400-
AnonConst,
401-
Const,
402-
Static,
403-
ForeignStatic,
404-
ForeignMod,
405-
ForeignType,
406-
GlobalAsm,
407-
Type,
408-
TypeParam,
409-
ConstParam,
410-
OpaqueTy,
411-
Enum,
412-
Field,
413-
Variant(LazyValue<VariantData>),
414-
Struct(LazyValue<VariantData>),
415-
Union(LazyValue<VariantData>),
416-
Fn,
417-
ForeignFn,
418-
Mod(LazyArray<ModChild>),
419-
MacroDef(LazyValue<ast::MacArgs>, /*macro_rules*/ bool),
420-
ProcMacro(MacroKind),
421-
Closure,
422-
Generator,
423-
Trait,
424-
Impl,
425-
AssocFn { container: ty::AssocItemContainer, has_self: bool },
426-
AssocType(ty::AssocItemContainer),
427-
AssocConst(ty::AssocItemContainer),
428-
TraitAlias,
395+
variant_data: Table<DefIndex, LazyValue<VariantData>>,
396+
assoc_container: Table<DefIndex, ty::AssocItemContainer>,
397+
// Slot is full when macro is macro_rules.
398+
macro_rules: Table<DefIndex, ()>,
399+
macro_definition: Table<DefIndex, LazyValue<ast::MacArgs>>,
400+
proc_macro: Table<DefIndex, MacroKind>,
401+
module_reexports: Table<DefIndex, LazyArray<ModChild>>,
429402
}
430403

431404
#[derive(TyEncodable, TyDecodable)]
@@ -459,7 +432,6 @@ pub fn provide(providers: &mut Providers) {
459432

460433
trivially_parameterized_over_tcx! {
461434
VariantData,
462-
EntryKind,
463435
RawDefId,
464436
TraitImpls,
465437
IncoherentImpls,

‎compiler/rustc_metadata/src/rmeta/table.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ macro_rules! fixed_size_enum {
5050
}
5151
match b[0] - 1 {
5252
$(${index()} => Some($($pat)*),)*
53-
_ => panic!("Unexpected ImplPolarity code: {:?}", b[0]),
53+
_ => panic!("Unexpected {} code: {:?}", stringify!($ty), b[0]),
5454
}
5555
}
5656

@@ -140,6 +140,21 @@ fixed_size_enum! {
140140
}
141141
}
142142

143+
fixed_size_enum! {
144+
ty::AssocItemContainer {
145+
( TraitContainer )
146+
( ImplContainer )
147+
}
148+
}
149+
150+
fixed_size_enum! {
151+
MacroKind {
152+
( Attr )
153+
( Bang )
154+
( Derive )
155+
}
156+
}
157+
143158
// We directly encode `DefPathHash` because a `LazyValue` would incur a 25% cost.
144159
impl FixedSizeEncoding for Option<DefPathHash> {
145160
type ByteArray = [u8; 16];

‎compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ trivially_parameterized_over_tcx! {
5555
crate::middle::exported_symbols::SymbolExportInfo,
5656
crate::middle::resolve_lifetime::ObjectLifetimeDefault,
5757
crate::mir::ConstQualifs,
58+
ty::AssocItemContainer,
5859
ty::Generics,
5960
ty::ImplPolarity,
6061
ty::ReprOptions,

‎compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10291029
self.insert_field_names(def_id, field_names);
10301030
}
10311031
Res::Def(DefKind::AssocFn, def_id) => {
1032-
if cstore.fn_has_self_parameter_untracked(def_id) {
1032+
if cstore.fn_has_self_parameter_untracked(def_id, self.r.session) {
10331033
self.r.has_self.insert(def_id);
10341034
}
10351035
}

0 commit comments

Comments
 (0)
Please sign in to comment.