Remove EntryKind.

This commit is contained in:
Camille GILLOT 2022-07-03 18:02:35 +02:00
parent affb12210d
commit c485fccd81
3 changed files with 32 additions and 148 deletions

View File

@ -785,26 +785,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
self.opt_item_ident(item_index, sess).expect("no encoded ident for item")
}
fn maybe_kind(self, item_id: DefIndex) -> Option<EntryKind> {
self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
}
#[inline]
pub(super) fn map_encoded_cnum_to_current(self, cnum: CrateNum) -> CrateNum {
if cnum == LOCAL_CRATE { self.cnum } else { self.cnum_map[cnum] }
}
fn kind(self, item_id: DefIndex) -> EntryKind {
self.maybe_kind(item_id).unwrap_or_else(|| {
bug!(
"CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
item_id,
self.root.name,
self.cnum,
)
})
}
fn def_kind(self, item_id: DefIndex) -> DefKind {
self.root.tables.opt_def_kind.get(self, item_id).unwrap_or_else(|| {
bug!(
@ -856,11 +841,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
)
}
fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
fn get_variant(self, kind: &DefKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
let adt_kind = match kind {
EntryKind::Variant => ty::AdtKind::Enum,
EntryKind::Struct => ty::AdtKind::Struct,
EntryKind::Union => ty::AdtKind::Union,
DefKind::Variant => ty::AdtKind::Enum,
DefKind::Struct => ty::AdtKind::Struct,
DefKind::Union => ty::AdtKind::Union,
_ => bug!(),
};
@ -896,13 +881,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
fn get_adt_def(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::AdtDef<'tcx> {
let kind = self.kind(item_id);
let kind = self.def_kind(item_id);
let did = self.local_def_id(item_id);
let adt_kind = match kind {
EntryKind::Enum => ty::AdtKind::Enum,
EntryKind::Struct => ty::AdtKind::Struct,
EntryKind::Union => ty::AdtKind::Union,
DefKind::Enum => ty::AdtKind::Enum,
DefKind::Struct => ty::AdtKind::Struct,
DefKind::Union => ty::AdtKind::Union,
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
};
let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode(self);
@ -914,7 +899,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.get(self, item_id)
.unwrap_or_else(LazyArray::empty)
.decode(self)
.map(|index| self.get_variant(&self.kind(index), index, did))
.map(|index| self.get_variant(&self.def_kind(index), index, did))
.collect()
} else {
std::iter::once(self.get_variant(&kind, item_id, did)).collect()
@ -1129,10 +1114,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
let name = self.item_name(id);
let kind = match self.kind(id) {
EntryKind::AssocConst => ty::AssocKind::Const,
EntryKind::AssocFn => ty::AssocKind::Fn,
EntryKind::AssocType => ty::AssocKind::Type,
let kind = match self.def_kind(id) {
DefKind::AssocConst => ty::AssocKind::Const,
DefKind::AssocFn => ty::AssocKind::Fn,
DefKind::AssocTy => ty::AssocKind::Type,
_ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
};
let has_self = self.get_fn_has_self_parameter(id);
@ -1149,8 +1134,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
match self.kind(node_id) {
EntryKind::Struct | EntryKind::Variant => {
match self.def_kind(node_id) {
DefKind::Struct | DefKind::Variant => {
let vdata = self.root.tables.variant_data.get(self, node_id).unwrap().decode(self);
vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
}
@ -1339,8 +1324,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
fn get_macro(self, id: DefIndex, sess: &Session) -> ast::MacroDef {
match self.kind(id) {
EntryKind::MacroDef => {
match self.def_kind(id) {
DefKind::Macro(_) => {
self.root.tables.macro_definition.get(self, id).unwrap().decode((self, sess))
}
_ => bug!(),
@ -1348,9 +1333,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
fn is_foreign_item(self, id: DefIndex) -> bool {
match self.kind(id) {
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,
_ => false,
if let Some(parent) = self.def_key(id).parent {
matches!(self.def_kind(parent), DefKind::ForeignMod)
} else {
false
}
}

View File

@ -16,7 +16,6 @@ use rustc_hir::def_id::{
use rustc_hir::definitions::DefPathData;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::lang_items;
use rustc_hir::{AnonConst, GenericParamKind};
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::middle::exported_symbols::{
@ -1207,7 +1206,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};
record!(self.tables.variant_data[def_id] <- data);
record!(self.tables.kind[def_id] <- EntryKind::Variant);
self.tables.constness.set(def_id.index, hir::Constness::Const);
record_array!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
assert!(f.did.is_local());
@ -1236,7 +1234,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};
record!(self.tables.variant_data[def_id] <- data);
record!(self.tables.kind[def_id] <- EntryKind::Variant);
self.tables.constness.set(def_id.index, hir::Constness::Const);
if variant.ctor_kind == CtorKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
@ -1260,7 +1257,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};
record_array!(self.tables.module_reexports[def_id] <- reexports);
record!(self.tables.kind[def_id] <- EntryKind::Mod);
if self.is_proc_macro {
// Encode this here because we don't do it in encode_def_ids.
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
@ -1302,7 +1298,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.repr_options[def_id] <- adt_def.repr());
record!(self.tables.variant_data[def_id] <- data);
record!(self.tables.kind[def_id] <- EntryKind::Struct);
self.tables.constness.set(def_id.index, hir::Constness::Const);
if variant.ctor_kind == CtorKind::Fn {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
@ -1327,9 +1322,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.tables.assoc_container.set(def_id.index, trait_item.container);
match trait_item.kind {
ty::AssocKind::Const => {
record!(self.tables.kind[def_id] <- EntryKind::AssocConst);
}
ty::AssocKind::Const => {}
ty::AssocKind::Fn => {
let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind else { bug!() };
match *m {
@ -1345,11 +1338,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if trait_item.fn_has_self_parameter {
self.tables.fn_has_self_parameter.set(def_id.index, ());
}
record!(self.tables.kind[def_id] <- EntryKind::AssocFn );
}
ty::AssocKind::Type => {
self.encode_explicit_item_bounds(def_id);
record!(self.tables.kind[def_id] <- EntryKind::AssocType);
}
}
if trait_item.kind == ty::AssocKind::Fn {
@ -1367,9 +1358,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.tables.assoc_container.set(def_id.index, impl_item.container);
match impl_item.kind {
ty::AssocKind::Const => {
record!(self.tables.kind[def_id] <- EntryKind::AssocConst);
}
ty::AssocKind::Fn => {
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
self.tables.asyncness.set(def_id.index, sig.header.asyncness);
@ -1384,11 +1372,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if impl_item.fn_has_self_parameter {
self.tables.fn_has_self_parameter.set(def_id.index, ());
}
record!(self.tables.kind[def_id] <- EntryKind::AssocFn);
}
ty::AssocKind::Type => {
record!(self.tables.kind[def_id] <- EntryKind::AssocType);
}
ty::AssocKind::Const | ty::AssocKind::Type => {}
}
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
self.tables.trait_item_def_id.set(def_id.index, trait_item_def_id.into());
@ -1502,33 +1487,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_info_for_item({:?})", def_id);
let entry_kind = match item.kind {
hir::ItemKind::Static(..) => EntryKind::Static,
hir::ItemKind::Const(..) => EntryKind::Const,
match item.kind {
hir::ItemKind::Fn(ref sig, .., body) => {
self.tables.asyncness.set(def_id.index, sig.header.asyncness);
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
self.tables.constness.set(def_id.index, sig.header.constness);
EntryKind::Fn
}
hir::ItemKind::Macro(ref macro_def, _) => {
record!(self.tables.macro_definition[def_id] <- macro_def);
EntryKind::MacroDef
}
hir::ItemKind::Mod(ref m) => {
return self.encode_info_for_mod(item.def_id, m);
}
hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod,
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
hir::ItemKind::TyAlias(..) => EntryKind::Type,
hir::ItemKind::OpaqueTy(..) => {
self.encode_explicit_item_bounds(def_id);
EntryKind::OpaqueTy
}
hir::ItemKind::Enum(..) => {
let adt_def = self.tcx.adt_def(def_id);
record!(self.tables.repr_options[def_id] <- adt_def.repr());
EntryKind::Enum
}
hir::ItemKind::Struct(ref struct_def, _) => {
let adt_def = self.tcx.adt_def(def_id);
@ -1549,7 +1525,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
ctor,
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
});
EntryKind::Struct
}
hir::ItemKind::Union(..) => {
let adt_def = self.tcx.adt_def(def_id);
@ -1562,7 +1537,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
ctor: None,
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
});
EntryKind::Union
}
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
self.tables.impl_defaultness.set(def_id.index, *defaultness);
@ -1588,26 +1562,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let polarity = self.tcx.impl_polarity(def_id);
self.tables.impl_polarity.set(def_id.index, polarity);
EntryKind::Impl
}
hir::ItemKind::Trait(..) => {
let trait_def = self.tcx.trait_def(def_id);
record!(self.tables.trait_def[def_id] <- trait_def);
EntryKind::Trait
}
hir::ItemKind::TraitAlias(..) => {
let trait_def = self.tcx.trait_def(def_id);
record!(self.tables.trait_def[def_id] <- trait_def);
EntryKind::TraitAlias
}
hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {
bug!("cannot encode info for item {:?}", item)
}
hir::ItemKind::Static(..)
| hir::ItemKind::Const(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::GlobalAsm(..)
| hir::ItemKind::TyAlias(..) => {}
};
record!(self.tables.kind[def_id] <- entry_kind);
// FIXME(eddyb) there should be a nicer way to do this.
match item.kind {
hir::ItemKind::Enum(..) => record_array!(self.tables.children[def_id] <-
@ -1653,8 +1625,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
match item.kind {
hir::ItemKind::Enum(..) => {
let def = self.tcx.adt_def(item.def_id.to_def_id());
self.encode_fields(def);
for (i, variant) in def.variants().iter_enumerated() {
self.encode_enum_variant_info(def, i);
@ -1665,18 +1635,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
hir::ItemKind::Struct(ref struct_def, _) => {
let def = self.tcx.adt_def(item.def_id.to_def_id());
self.encode_fields(def);
// If the struct has a constructor, encode it.
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id);
self.encode_struct_ctor(def, ctor_def_id.to_def_id());
}
}
hir::ItemKind::Union(..) => {
let def = self.tcx.adt_def(item.def_id.to_def_id());
self.encode_fields(def);
}
hir::ItemKind::Impl { .. } => {
for &trait_item_def_id in
self.tcx.associated_item_def_ids(item.def_id.to_def_id()).iter()
@ -1705,13 +1669,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
ty::Generator(..) => {
let data = self.tcx.generator_kind(def_id).unwrap();
let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator);
record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
}
ty::Closure(_, substs) => {
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure);
record!(self.tables.fn_sig[def_id.to_def_id()] <- substs.as_closure().sig());
}
@ -1719,12 +1681,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst);
}
fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
empty_proc_macro!(self);
let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
@ -1821,7 +1777,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let def_id = id.to_def_id();
self.tables.opt_def_kind.set(def_id.index, DefKind::Macro(macro_kind));
self.tables.proc_macro.set(def_id.index, macro_kind);
record!(self.tables.kind[def_id] <- EntryKind::ProcMacro);
self.encode_attrs(id);
record!(self.tables.def_keys[def_id] <- def_key);
record!(self.tables.def_ident_span[def_id] <- span);
@ -2059,15 +2014,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
hir::Constness::NotConst
};
self.tables.constness.set(def_id.index, constness);
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn);
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
}
hir::ForeignItemKind::Static(..) => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
}
hir::ForeignItemKind::Type => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);
}
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => {}
}
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
if tcx.is_intrinsic(def_id) {
@ -2088,10 +2037,6 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> {
intravisit::walk_expr(self, ex);
self.encode_info_for_expr(ex);
}
fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
intravisit::walk_anon_const(self, c);
self.encode_info_for_anon_const(c.hir_id);
}
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
intravisit::walk_item(self, item);
match item.kind {
@ -2110,24 +2055,12 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> {
}
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_fields(&mut self, adt_def: ty::AdtDef<'tcx>) {
for (variant_index, variant) in adt_def.variants().iter_enumerated() {
for (field_index, _field) in variant.fields.iter().enumerate() {
let variant = &adt_def.variant(variant_index);
let field = &variant.fields[field_index];
let def_id = field.did;
debug!("EncodeContext::encode_field({:?})", def_id);
record!(self.tables.kind[def_id] <- EntryKind::Field);
}
}
}
fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) {
for param in generics.params {
let def_id = self.tcx.hir().local_def_id(param.hir_id);
match param.kind {
GenericParamKind::Lifetime { .. } | GenericParamKind::Type { .. } => {}
GenericParamKind::Const { ref default, .. } => {
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {}
hir::GenericParamKind::Const { ref default, .. } => {
let def_id = def_id.to_def_id();
if default.is_some() {
record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id))

View File

@ -334,7 +334,6 @@ macro_rules! define_tables {
}
define_tables! {
kind: Table<DefIndex, LazyValue<EntryKind>>,
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
children: Table<DefIndex, LazyArray<DefIndex>>,
@ -402,39 +401,6 @@ define_tables! {
module_reexports: Table<DefIndex, LazyArray<ModChild>>,
}
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
enum EntryKind {
AnonConst,
Const,
Static,
ForeignStatic,
ForeignMod,
ForeignType,
GlobalAsm,
Type,
TypeParam,
ConstParam,
OpaqueTy,
Enum,
Field,
Variant,
Struct,
Union,
Fn,
ForeignFn,
Mod,
MacroDef,
ProcMacro,
Closure,
Generator,
Trait,
Impl,
AssocFn,
AssocType,
AssocConst,
TraitAlias,
}
#[derive(TyEncodable, TyDecodable)]
struct VariantData {
ctor_kind: CtorKind,
@ -466,7 +432,6 @@ pub fn provide(providers: &mut Providers) {
trivially_parameterized_over_tcx! {
VariantData,
EntryKind,
RawDefId,
TraitImpls,
IncoherentImpls,