Skip to content

Commit 45ad22b

Browse files
committed
Encode type in the main loop.
1 parent 230a8ee commit 45ad22b

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+55-56
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,54 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
10201020
}
10211021
}
10221022

1023+
fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool {
1024+
match def_kind {
1025+
DefKind::Struct
1026+
| DefKind::Union
1027+
| DefKind::Enum
1028+
| DefKind::Variant
1029+
| DefKind::Ctor(..)
1030+
| DefKind::Field
1031+
| DefKind::Fn
1032+
| DefKind::Const
1033+
| DefKind::Static(..)
1034+
| DefKind::TyAlias
1035+
| DefKind::OpaqueTy
1036+
| DefKind::ForeignTy
1037+
| DefKind::Impl
1038+
| DefKind::AssocFn
1039+
| DefKind::AssocConst
1040+
| DefKind::Closure
1041+
| DefKind::Generator
1042+
| DefKind::ConstParam
1043+
| DefKind::AnonConst
1044+
| DefKind::InlineConst => true,
1045+
1046+
DefKind::AssocTy => {
1047+
let assoc_item = tcx.associated_item(def_id);
1048+
match assoc_item.container {
1049+
ty::AssocItemContainer::ImplContainer => true,
1050+
ty::AssocItemContainer::TraitContainer => assoc_item.defaultness(tcx).has_value(),
1051+
}
1052+
}
1053+
DefKind::TyParam => {
1054+
let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(def_id) else { bug!() };
1055+
let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() };
1056+
default.is_some()
1057+
}
1058+
1059+
DefKind::Trait
1060+
| DefKind::TraitAlias
1061+
| DefKind::Mod
1062+
| DefKind::ForeignMod
1063+
| DefKind::Macro(..)
1064+
| DefKind::Use
1065+
| DefKind::LifetimeParam
1066+
| DefKind::GlobalAsm
1067+
| DefKind::ExternCrate => false,
1068+
}
1069+
}
1070+
10231071
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10241072
fn encode_attrs(&mut self, def_id: LocalDefId) {
10251073
let mut attrs = self
@@ -1076,6 +1124,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10761124
record_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
10771125
}
10781126
}
1127+
if should_encode_type(tcx, local_id, def_kind) {
1128+
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
1129+
}
10791130
if let DefKind::TyParam | DefKind::ConstParam = def_kind {
10801131
if let Some(default) = self.tcx.object_lifetime_default(def_id) {
10811132
record!(self.tables.object_lifetime_default[def_id] <- default);
@@ -1097,11 +1148,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10971148
}
10981149
}
10991150

1100-
fn encode_item_type(&mut self, def_id: DefId) {
1101-
debug!("EncodeContext::encode_item_type({:?})", def_id);
1102-
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
1103-
}
1104-
11051151
fn encode_enum_variant_info(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) {
11061152
let tcx = self.tcx;
11071153
let variant = &def.variant(index);
@@ -1121,7 +1167,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11211167
assert!(f.did.is_local());
11221168
f.did.index
11231169
}));
1124-
self.encode_item_type(def_id);
11251170
if variant.ctor_kind == CtorKind::Fn {
11261171
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
11271172
if let Some(ctor_def_id) = variant.ctor_def_id {
@@ -1146,7 +1191,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11461191

11471192
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
11481193
self.tables.constness.set(def_id.index, hir::Constness::Const);
1149-
self.encode_item_type(def_id);
11501194
if variant.ctor_kind == CtorKind::Fn {
11511195
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
11521196
}
@@ -1212,7 +1256,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12121256
debug!("EncodeContext::encode_field({:?})", def_id);
12131257

12141258
record!(self.tables.kind[def_id] <- EntryKind::Field);
1215-
self.encode_item_type(def_id);
12161259
}
12171260

12181261
fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>, def_id: DefId) {
@@ -1230,7 +1273,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12301273
record!(self.tables.repr_options[def_id] <- adt_def.repr());
12311274
self.tables.constness.set(def_id.index, hir::Constness::Const);
12321275
record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data)));
1233-
self.encode_item_type(def_id);
12341276
if variant.ctor_kind == CtorKind::Fn {
12351277
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
12361278
}
@@ -1285,16 +1327,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12851327
record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::TraitContainer));
12861328
}
12871329
}
1288-
match trait_item.kind {
1289-
ty::AssocKind::Const | ty::AssocKind::Fn => {
1290-
self.encode_item_type(def_id);
1291-
}
1292-
ty::AssocKind::Type => {
1293-
if ast_item.defaultness.has_value() {
1294-
self.encode_item_type(def_id);
1295-
}
1296-
}
1297-
}
12981330
if trait_item.kind == ty::AssocKind::Fn {
12991331
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
13001332
}
@@ -1341,7 +1373,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13411373
record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::ImplContainer));
13421374
}
13431375
}
1344-
self.encode_item_type(def_id);
13451376
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
13461377
self.tables.trait_item_def_id.set(def_id.index, trait_item_def_id.into());
13471378
}
@@ -1590,18 +1621,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15901621
}
15911622
_ => {}
15921623
}
1593-
match item.kind {
1594-
hir::ItemKind::Static(..)
1595-
| hir::ItemKind::Const(..)
1596-
| hir::ItemKind::Fn(..)
1597-
| hir::ItemKind::TyAlias(..)
1598-
| hir::ItemKind::OpaqueTy(..)
1599-
| hir::ItemKind::Enum(..)
1600-
| hir::ItemKind::Struct(..)
1601-
| hir::ItemKind::Union(..)
1602-
| hir::ItemKind::Impl { .. } => self.encode_item_type(def_id),
1603-
_ => {}
1604-
}
16051624
if let hir::ItemKind::Fn(..) = item.kind {
16061625
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
16071626
if tcx.is_intrinsic(def_id) {
@@ -1615,13 +1634,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16151634
}
16161635
}
16171636

1618-
fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) {
1619-
record!(self.tables.kind[def_id] <- kind);
1620-
if encode_type {
1621-
self.encode_item_type(def_id);
1622-
}
1623-
}
1624-
16251637
fn encode_info_for_closure(&mut self, hir_id: hir::HirId) {
16261638
let def_id = self.tcx.hir().local_def_id(hir_id);
16271639
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
@@ -1638,16 +1650,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16381650
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
16391651
}
16401652

1641-
ty::Closure(..) => {
1653+
ty::Closure(_, substs) => {
16421654
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure);
1655+
record!(self.tables.fn_sig[def_id.to_def_id()] <- substs.as_closure().sig());
16431656
}
16441657

16451658
_ => bug!("closure that is neither generator nor closure"),
16461659
}
1647-
self.encode_item_type(def_id.to_def_id());
1648-
if let ty::Closure(def_id, substs) = *ty.kind() {
1649-
record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig());
1650-
}
16511660
}
16521661

16531662
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
@@ -1660,7 +1669,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16601669
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst);
16611670
record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
16621671
record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
1663-
self.encode_item_type(def_id.to_def_id());
16641672
}
16651673

16661674
fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
@@ -1997,6 +2005,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19972005
};
19982006
self.tables.constness.set(def_id.index, constness);
19992007
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn);
2008+
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
20002009
}
20012010
hir::ForeignItemKind::Static(..) => {
20022011
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
@@ -2005,9 +2014,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20052014
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);
20062015
}
20072016
}
2008-
self.encode_item_type(def_id);
20092017
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
2010-
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
20112018
if tcx.is_intrinsic(def_id) {
20122019
self.tables.is_intrinsic.set(def_id.index, ());
20132020
}
@@ -2061,17 +2068,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20612068
for param in generics.params {
20622069
let def_id = self.tcx.hir().local_def_id(param.hir_id);
20632070
match param.kind {
2064-
GenericParamKind::Lifetime { .. } => continue,
2065-
GenericParamKind::Type { default, .. } => {
2066-
self.encode_info_for_generic_param(
2067-
def_id.to_def_id(),
2068-
EntryKind::TypeParam,
2069-
default.is_some(),
2070-
);
2071-
}
2071+
GenericParamKind::Lifetime { .. } | GenericParamKind::Type { .. } => {}
20722072
GenericParamKind::Const { ref default, .. } => {
20732073
let def_id = def_id.to_def_id();
2074-
self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true);
20752074
if default.is_some() {
20762075
record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id))
20772076
}

0 commit comments

Comments
 (0)