@@ -38,7 +38,6 @@ use rustc_span::symbol::{sym, Symbol};
38
38
use rustc_span:: {
39
39
self , DebuggerVisualizerFile , ExternalSource , FileName , SourceFile , Span , SyntaxContext ,
40
40
} ;
41
- use rustc_target:: abi:: VariantIdx ;
42
41
use std:: borrow:: Borrow ;
43
42
use std:: collections:: hash_map:: Entry ;
44
43
use std:: hash:: Hash ;
@@ -1189,8 +1188,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1189
1188
record ! ( self . tables. super_predicates_of[ def_id] <- self . tcx. super_predicates_of( def_id) ) ;
1190
1189
}
1191
1190
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) ;
1194
1192
}
1195
1193
if should_encode_trait_impl_trait_tys ( tcx, def_id)
1196
1194
&& 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> {
1213
1211
}
1214
1212
}
1215
1213
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 ) {
1217
1216
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( ) ) ;
1227
1219
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.
1237
1238
}
1238
- }
1239
1239
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) ;
1244
1247
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
+ } ) ) ;
1251
1253
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
+ }
1256
1261
}
1257
1262
}
1258
1263
@@ -1305,25 +1310,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1305
1310
}
1306
1311
}
1307
1312
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
-
1327
1313
fn encode_explicit_item_bounds ( & mut self , def_id : DefId ) {
1328
1314
debug ! ( "EncodeContext::encode_explicit_item_bounds({:?})" , def_id) ;
1329
1315
let bounds = self . tcx . explicit_item_bounds ( def_id) ;
@@ -1532,33 +1518,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1532
1518
self . tables . is_type_alias_impl_trait . set_nullable ( def_id. index , true ) ;
1533
1519
}
1534
1520
}
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
- }
1562
1521
hir:: ItemKind :: Impl ( hir:: Impl { defaultness, constness, .. } ) => {
1563
1522
self . tables . impl_defaultness . set ( def_id. index , * defaultness) ;
1564
1523
self . tables . constness . set ( def_id. index , * constness) ;
@@ -1597,31 +1556,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1597
1556
}
1598
1557
hir:: ItemKind :: Static ( ..)
1599
1558
| hir:: ItemKind :: Const ( ..)
1559
+ | hir:: ItemKind :: Enum ( ..)
1560
+ | hir:: ItemKind :: Struct ( ..)
1561
+ | hir:: ItemKind :: Union ( ..)
1600
1562
| hir:: ItemKind :: ForeignMod { .. }
1601
1563
| hir:: ItemKind :: GlobalAsm ( ..)
1602
1564
| hir:: ItemKind :: TyAlias ( ..) => { }
1603
1565
} ;
1604
1566
// FIXME(eddyb) there should be a nicer way to do this.
1605
1567
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
- }
1625
1568
hir:: ItemKind :: Impl { .. } | hir:: ItemKind :: Trait ( ..) => {
1626
1569
let associated_item_def_ids = self . tcx . associated_item_def_ids ( def_id) ;
1627
1570
record_array ! ( self . tables. children[ def_id] <-
@@ -1649,17 +1592,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1649
1592
// so it's easier to do that here then to wait until we would encounter
1650
1593
// normally in the visitor walk.
1651
1594
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
- }
1663
1595
hir:: ItemKind :: Impl { .. } => {
1664
1596
for & trait_item_def_id in
1665
1597
self . tcx . associated_item_def_ids ( item. owner_id . to_def_id ( ) ) . iter ( )
0 commit comments