Skip to content

Commit d7f6564

Browse files
committed
Encode AdtDef in the def-id loop.
1 parent b22aa57 commit d7f6564

File tree

1 file changed

+41
-100
lines changed

1 file changed

+41
-100
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+41-100
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use rustc_span::symbol::{sym, Symbol};
3838
use rustc_span::{
3939
self, DebuggerVisualizerFile, ExternalSource, FileName, SourceFile, Span, SyntaxContext,
4040
};
41-
use rustc_target::abi::VariantIdx;
4241
use std::borrow::Borrow;
4342
use std::collections::hash_map::Entry;
4443
use std::hash::Hash;
@@ -1178,8 +1177,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11781177
record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
11791178
}
11801179
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
1181-
let params_in_repr = self.tcx.params_in_repr(def_id);
1182-
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1180+
self.encode_info_for_adt(def_id);
11831181
}
11841182
if should_encode_trait_impl_trait_tys(tcx, def_id)
11851183
&& let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
@@ -1199,9 +1197,38 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11991197
}
12001198
}
12011199

1202-
fn encode_enum_variant_info(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) {
1200+
#[instrument(level = "trace", skip(self))]
1201+
fn encode_info_for_adt(&mut self, def_id: DefId) {
1202+
let tcx = self.tcx;
1203+
let adt_def = tcx.adt_def(def_id);
1204+
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1205+
1206+
let params_in_repr = self.tcx.params_in_repr(def_id);
1207+
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1208+
1209+
if adt_def.is_enum() {
1210+
record_array!(self.tables.children[def_id] <- iter::from_generator(||
1211+
for variant in tcx.adt_def(def_id).variants() {
1212+
yield variant.def_id.index;
1213+
// Encode constructors which take a separate slot in value namespace.
1214+
if let Some(ctor_def_id) = variant.ctor_def_id() {
1215+
yield ctor_def_id.index;
1216+
}
1217+
}
1218+
));
1219+
}
1220+
1221+
// In some cases, along with the item itself, we also
1222+
// encode some sub-items. Usually we want some info from the item
1223+
// so it's easier to do that here then to wait until we would encounter
1224+
// normally in the visitor walk.
1225+
for variant in adt_def.variants().iter() {
1226+
self.encode_enum_variant_info(variant);
1227+
}
1228+
}
1229+
1230+
fn encode_enum_variant_info(&mut self, variant: &ty::VariantDef) {
12031231
let tcx = self.tcx;
1204-
let variant = &def.variant(index);
12051232
let def_id = variant.def_id;
12061233
debug!("EncodeContext::encode_enum_variant_info({:?})", def_id);
12071234

@@ -1218,27 +1245,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12181245
f.did.index
12191246
}));
12201247
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1221-
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
1222-
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
1223-
}
1224-
}
1225-
1226-
fn encode_enum_variant_ctor(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) {
1227-
let variant = &def.variant(index);
1228-
let Some((ctor_kind, def_id)) = variant.ctor else { return };
1229-
debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);
1248+
debug!("EncodeContext::encode_enum_variant_ctor({:?})", ctor_def_id);
12301249

1231-
// FIXME(eddyb) encode only the `CtorKind` for constructors.
1232-
let data = VariantData {
1233-
discr: variant.discr,
1234-
ctor: Some((ctor_kind, def_id.index)),
1235-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1236-
};
1250+
self.tables.constness.set(ctor_def_id.index, hir::Constness::Const);
12371251

1238-
record!(self.tables.variant_data[def_id] <- data);
1239-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1240-
if ctor_kind == CtorKind::Fn {
1241-
record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id));
1252+
let fn_sig = tcx.fn_sig(ctor_def_id);
1253+
record!(self.tables.fn_sig[ctor_def_id] <- fn_sig);
1254+
// FIXME(eddyb) encode signature only for `ctor_def_id`.
1255+
record!(self.tables.fn_sig[def_id] <- fn_sig);
12421256
}
12431257
}
12441258

@@ -1291,25 +1305,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12911305
}
12921306
}
12931307

1294-
fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>) {
1295-
let variant = adt_def.non_enum_variant();
1296-
let Some((ctor_kind, def_id)) = variant.ctor else { return };
1297-
debug!("EncodeContext::encode_struct_ctor({:?})", def_id);
1298-
1299-
let data = VariantData {
1300-
discr: variant.discr,
1301-
ctor: Some((ctor_kind, def_id.index)),
1302-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1303-
};
1304-
1305-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1306-
record!(self.tables.variant_data[def_id] <- data);
1307-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1308-
if ctor_kind == CtorKind::Fn {
1309-
record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id));
1310-
}
1311-
}
1312-
13131308
fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
13141309
debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
13151310
let bounds = self.tcx.explicit_item_bounds(def_id);
@@ -1518,33 +1513,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15181513
self.tables.is_type_alias_impl_trait.set(def_id.index, ());
15191514
}
15201515
}
1521-
hir::ItemKind::Enum(..) => {
1522-
let adt_def = self.tcx.adt_def(def_id);
1523-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1524-
}
1525-
hir::ItemKind::Struct(..) => {
1526-
let adt_def = self.tcx.adt_def(def_id);
1527-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1528-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1529-
1530-
let variant = adt_def.non_enum_variant();
1531-
record!(self.tables.variant_data[def_id] <- VariantData {
1532-
discr: variant.discr,
1533-
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1534-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1535-
});
1536-
}
1537-
hir::ItemKind::Union(..) => {
1538-
let adt_def = self.tcx.adt_def(def_id);
1539-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1540-
1541-
let variant = adt_def.non_enum_variant();
1542-
record!(self.tables.variant_data[def_id] <- VariantData {
1543-
discr: variant.discr,
1544-
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1545-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1546-
});
1547-
}
15481516
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
15491517
self.tables.impl_defaultness.set(def_id.index, *defaultness);
15501518
self.tables.constness.set(def_id.index, *constness);
@@ -1583,31 +1551,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15831551
}
15841552
hir::ItemKind::Static(..)
15851553
| hir::ItemKind::Const(..)
1554+
| hir::ItemKind::Enum(..)
1555+
| hir::ItemKind::Struct(..)
1556+
| hir::ItemKind::Union(..)
15861557
| hir::ItemKind::ForeignMod { .. }
15871558
| hir::ItemKind::GlobalAsm(..)
15881559
| hir::ItemKind::TyAlias(..) => {}
15891560
};
15901561
// FIXME(eddyb) there should be a nicer way to do this.
15911562
match item.kind {
1592-
hir::ItemKind::Enum(..) => {
1593-
record_array!(self.tables.children[def_id] <- iter::from_generator(||
1594-
for variant in tcx.adt_def(def_id).variants() {
1595-
yield variant.def_id.index;
1596-
// Encode constructors which take a separate slot in value namespace.
1597-
if let Some(ctor_def_id) = variant.ctor_def_id() {
1598-
yield ctor_def_id.index;
1599-
}
1600-
}
1601-
))
1602-
}
1603-
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
1604-
record_array!(self.tables.children[def_id] <-
1605-
self.tcx.adt_def(def_id).non_enum_variant().fields.iter().map(|f| {
1606-
assert!(f.did.is_local());
1607-
f.did.index
1608-
})
1609-
)
1610-
}
16111563
hir::ItemKind::Impl { .. } | hir::ItemKind::Trait(..) => {
16121564
let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
16131565
record_array!(self.tables.children[def_id] <-
@@ -1635,17 +1587,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16351587
// so it's easier to do that here then to wait until we would encounter
16361588
// normally in the visitor walk.
16371589
match item.kind {
1638-
hir::ItemKind::Enum(..) => {
1639-
let def = self.tcx.adt_def(item.owner_id.to_def_id());
1640-
for (i, _) in def.variants().iter_enumerated() {
1641-
self.encode_enum_variant_info(def, i);
1642-
self.encode_enum_variant_ctor(def, i);
1643-
}
1644-
}
1645-
hir::ItemKind::Struct(..) => {
1646-
let def = self.tcx.adt_def(item.owner_id.to_def_id());
1647-
self.encode_struct_ctor(def);
1648-
}
16491590
hir::ItemKind::Impl { .. } => {
16501591
for &trait_item_def_id in
16511592
self.tcx.associated_item_def_ids(item.owner_id.to_def_id()).iter()

0 commit comments

Comments
 (0)