Skip to content

Commit ca7a610

Browse files
authored
Rollup merge of rust-lang#107189 - cjgillot:meta-adt, r=compiler-errors
Encode info for Adt in a single place. Split from rust-lang#98867
2 parents 2f2356a + 4d11206 commit ca7a610

File tree

1 file changed

+45
-113
lines changed

1 file changed

+45
-113
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+45-113
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;
@@ -1189,8 +1188,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11891188
record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
11901189
}
11911190
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
1192-
let params_in_repr = self.tcx.params_in_repr(def_id);
1193-
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1191+
self.encode_info_for_adt(def_id);
11941192
}
11951193
if should_encode_trait_impl_trait_tys(tcx, def_id)
11961194
&& let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
@@ -1213,46 +1211,53 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12131211
}
12141212
}
12151213

1216-
fn encode_enum_variant_info(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) {
1214+
#[instrument(level = "trace", skip(self))]
1215+
fn encode_info_for_adt(&mut self, def_id: DefId) {
12171216
let tcx = self.tcx;
1218-
let variant = &def.variant(index);
1219-
let def_id = variant.def_id;
1220-
debug!("EncodeContext::encode_enum_variant_info({:?})", def_id);
1221-
1222-
let data = VariantData {
1223-
discr: variant.discr,
1224-
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1225-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1226-
};
1217+
let adt_def = tcx.adt_def(def_id);
1218+
record!(self.tables.repr_options[def_id] <- adt_def.repr());
12271219

1228-
record!(self.tables.variant_data[def_id] <- data);
1229-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1230-
record_array!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
1231-
assert!(f.did.is_local());
1232-
f.did.index
1233-
}));
1234-
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1235-
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
1236-
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
1220+
let params_in_repr = self.tcx.params_in_repr(def_id);
1221+
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
1222+
1223+
if adt_def.is_enum() {
1224+
record_array!(self.tables.children[def_id] <- iter::from_generator(||
1225+
for variant in tcx.adt_def(def_id).variants() {
1226+
yield variant.def_id.index;
1227+
// Encode constructors which take a separate slot in value namespace.
1228+
if let Some(ctor_def_id) = variant.ctor_def_id() {
1229+
yield ctor_def_id.index;
1230+
}
1231+
}
1232+
));
1233+
} else {
1234+
// For non-enum, there is only one variant, and its def_id is the adt's.
1235+
debug_assert_eq!(adt_def.variants().len(), 1);
1236+
debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id);
1237+
// Therefore, the loop over variants will encode its fields as the adt's children.
12371238
}
1238-
}
12391239

1240-
fn encode_enum_variant_ctor(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) {
1241-
let variant = &def.variant(index);
1242-
let Some((ctor_kind, def_id)) = variant.ctor else { return };
1243-
debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);
1240+
for variant in adt_def.variants().iter() {
1241+
let data = VariantData {
1242+
discr: variant.discr,
1243+
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1244+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1245+
};
1246+
record!(self.tables.variant_data[variant.def_id] <- data);
12441247

1245-
// FIXME(eddyb) encode only the `CtorKind` for constructors.
1246-
let data = VariantData {
1247-
discr: variant.discr,
1248-
ctor: Some((ctor_kind, def_id.index)),
1249-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1250-
};
1248+
self.tables.constness.set(variant.def_id.index, hir::Constness::Const);
1249+
record_array!(self.tables.children[variant.def_id] <- variant.fields.iter().map(|f| {
1250+
assert!(f.did.is_local());
1251+
f.did.index
1252+
}));
12511253

1252-
record!(self.tables.variant_data[def_id] <- data);
1253-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1254-
if ctor_kind == CtorKind::Fn {
1255-
record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id));
1254+
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1255+
self.tables.constness.set(ctor_def_id.index, hir::Constness::Const);
1256+
let fn_sig = tcx.fn_sig(ctor_def_id);
1257+
record!(self.tables.fn_sig[ctor_def_id] <- fn_sig);
1258+
// FIXME only encode signature for ctor_def_id
1259+
record!(self.tables.fn_sig[variant.def_id] <- fn_sig);
1260+
}
12561261
}
12571262
}
12581263

@@ -1305,25 +1310,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13051310
}
13061311
}
13071312

1308-
fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>) {
1309-
let variant = adt_def.non_enum_variant();
1310-
let Some((ctor_kind, def_id)) = variant.ctor else { return };
1311-
debug!("EncodeContext::encode_struct_ctor({:?})", def_id);
1312-
1313-
let data = VariantData {
1314-
discr: variant.discr,
1315-
ctor: Some((ctor_kind, def_id.index)),
1316-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1317-
};
1318-
1319-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1320-
record!(self.tables.variant_data[def_id] <- data);
1321-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1322-
if ctor_kind == CtorKind::Fn {
1323-
record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id));
1324-
}
1325-
}
1326-
13271313
fn encode_explicit_item_bounds(&mut self, def_id: DefId) {
13281314
debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id);
13291315
let bounds = self.tcx.explicit_item_bounds(def_id);
@@ -1532,33 +1518,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15321518
self.tables.is_type_alias_impl_trait.set_nullable(def_id.index, true);
15331519
}
15341520
}
1535-
hir::ItemKind::Enum(..) => {
1536-
let adt_def = self.tcx.adt_def(def_id);
1537-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1538-
}
1539-
hir::ItemKind::Struct(..) => {
1540-
let adt_def = self.tcx.adt_def(def_id);
1541-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1542-
self.tables.constness.set(def_id.index, hir::Constness::Const);
1543-
1544-
let variant = adt_def.non_enum_variant();
1545-
record!(self.tables.variant_data[def_id] <- VariantData {
1546-
discr: variant.discr,
1547-
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1548-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1549-
});
1550-
}
1551-
hir::ItemKind::Union(..) => {
1552-
let adt_def = self.tcx.adt_def(def_id);
1553-
record!(self.tables.repr_options[def_id] <- adt_def.repr());
1554-
1555-
let variant = adt_def.non_enum_variant();
1556-
record!(self.tables.variant_data[def_id] <- VariantData {
1557-
discr: variant.discr,
1558-
ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)),
1559-
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
1560-
});
1561-
}
15621521
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
15631522
self.tables.impl_defaultness.set(def_id.index, *defaultness);
15641523
self.tables.constness.set(def_id.index, *constness);
@@ -1597,31 +1556,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15971556
}
15981557
hir::ItemKind::Static(..)
15991558
| hir::ItemKind::Const(..)
1559+
| hir::ItemKind::Enum(..)
1560+
| hir::ItemKind::Struct(..)
1561+
| hir::ItemKind::Union(..)
16001562
| hir::ItemKind::ForeignMod { .. }
16011563
| hir::ItemKind::GlobalAsm(..)
16021564
| hir::ItemKind::TyAlias(..) => {}
16031565
};
16041566
// FIXME(eddyb) there should be a nicer way to do this.
16051567
match item.kind {
1606-
hir::ItemKind::Enum(..) => {
1607-
record_array!(self.tables.children[def_id] <- iter::from_generator(||
1608-
for variant in tcx.adt_def(def_id).variants() {
1609-
yield variant.def_id.index;
1610-
// Encode constructors which take a separate slot in value namespace.
1611-
if let Some(ctor_def_id) = variant.ctor_def_id() {
1612-
yield ctor_def_id.index;
1613-
}
1614-
}
1615-
))
1616-
}
1617-
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
1618-
record_array!(self.tables.children[def_id] <-
1619-
self.tcx.adt_def(def_id).non_enum_variant().fields.iter().map(|f| {
1620-
assert!(f.did.is_local());
1621-
f.did.index
1622-
})
1623-
)
1624-
}
16251568
hir::ItemKind::Impl { .. } | hir::ItemKind::Trait(..) => {
16261569
let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
16271570
record_array!(self.tables.children[def_id] <-
@@ -1649,17 +1592,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16491592
// so it's easier to do that here then to wait until we would encounter
16501593
// normally in the visitor walk.
16511594
match item.kind {
1652-
hir::ItemKind::Enum(..) => {
1653-
let def = self.tcx.adt_def(item.owner_id.to_def_id());
1654-
for (i, _) in def.variants().iter_enumerated() {
1655-
self.encode_enum_variant_info(def, i);
1656-
self.encode_enum_variant_ctor(def, i);
1657-
}
1658-
}
1659-
hir::ItemKind::Struct(..) => {
1660-
let def = self.tcx.adt_def(item.owner_id.to_def_id());
1661-
self.encode_struct_ctor(def);
1662-
}
16631595
hir::ItemKind::Impl { .. } => {
16641596
for &trait_item_def_id in
16651597
self.tcx.associated_item_def_ids(item.owner_id.to_def_id()).iter()

0 commit comments

Comments
 (0)