@@ -7,6 +7,7 @@ use rustc_hir::def::CtorKind;
7
7
use rustc_hir:: def_id:: DefId ;
8
8
use rustc_index:: IndexVec ;
9
9
use rustc_middle:: middle:: stability;
10
+ use rustc_middle:: query:: Key ;
10
11
use rustc_middle:: ty:: { self , TyCtxt } ;
11
12
use rustc_span:: hygiene:: MacroKind ;
12
13
use rustc_span:: symbol:: { kw, sym, Symbol } ;
@@ -1249,6 +1250,9 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
1249
1250
match inner_type {
1250
1251
clean:: TypeAliasInnerType :: Enum { variants, is_non_exhaustive } => {
1251
1252
let variants_iter = || variants. iter ( ) . filter ( |i| !i. is_stripped ( ) ) ;
1253
+ let ty = cx. tcx ( ) . type_of ( it. def_id ( ) . unwrap ( ) ) . instantiate_identity ( ) ;
1254
+ let enum_def_id = ty. ty_adt_id ( ) . unwrap ( ) ;
1255
+
1252
1256
wrap_item ( w, |w| {
1253
1257
let variants_len = variants. len ( ) ;
1254
1258
let variants_count = variants_iter ( ) . count ( ) ;
@@ -1263,10 +1267,10 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
1263
1267
variants_count,
1264
1268
has_stripped_entries,
1265
1269
* is_non_exhaustive,
1266
- it . def_id ( ) . unwrap ( ) ,
1270
+ enum_def_id ,
1267
1271
)
1268
1272
} ) ;
1269
- item_variants ( w, cx, it, & variants) ;
1273
+ item_variants ( w, cx, it, & variants, enum_def_id ) ;
1270
1274
}
1271
1275
clean:: TypeAliasInnerType :: Union { fields } => {
1272
1276
wrap_item ( w, |w| {
@@ -1435,16 +1439,21 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
1435
1439
write ! ( w, "{}" , document( cx, it, None , HeadingOffset :: H2 ) ) ;
1436
1440
1437
1441
if count_variants != 0 {
1438
- item_variants ( w, cx, it, & e. variants ) ;
1442
+ item_variants ( w, cx, it, & e. variants , it . def_id ( ) . unwrap ( ) ) ;
1439
1443
}
1440
1444
let def_id = it. item_id . expect_def_id ( ) ;
1441
1445
write ! ( w, "{}" , render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ) ;
1442
1446
write ! ( w, "{}" , document_type_layout( cx, def_id) ) ;
1443
1447
}
1444
1448
1445
- /// It'll return true if all variants are C-like variants and if at least one of them has a value
1446
- /// set.
1447
- fn should_show_enum_discriminant ( variants : & IndexVec < VariantIdx , clean:: Item > ) -> bool {
1449
+ /// It'll return false if any variant is not a C-like variant. Otherwise it'll return true if at
1450
+ /// least one of them has an explicit discriminant or if the enum has `#[repr(C)]` or an integer
1451
+ /// `repr`.
1452
+ fn should_show_enum_discriminant (
1453
+ cx : & Context < ' _ > ,
1454
+ enum_def_id : DefId ,
1455
+ variants : & IndexVec < VariantIdx , clean:: Item > ,
1456
+ ) -> bool {
1448
1457
let mut has_variants_with_value = false ;
1449
1458
for variant in variants {
1450
1459
if let clean:: VariantItem ( ref var) = * variant. kind &&
@@ -1455,7 +1464,11 @@ fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -
1455
1464
return false ;
1456
1465
}
1457
1466
}
1458
- has_variants_with_value
1467
+ if has_variants_with_value {
1468
+ return true ;
1469
+ }
1470
+ let repr = cx. tcx ( ) . adt_def ( enum_def_id) . repr ( ) ;
1471
+ repr. c ( ) || repr. int . is_some ( )
1459
1472
}
1460
1473
1461
1474
fn display_c_like_variant (
@@ -1493,7 +1506,7 @@ fn render_enum_fields(
1493
1506
is_non_exhaustive : bool ,
1494
1507
enum_def_id : DefId ,
1495
1508
) {
1496
- let should_show_enum_discriminant = should_show_enum_discriminant ( variants) ;
1509
+ let should_show_enum_discriminant = should_show_enum_discriminant ( cx , enum_def_id , variants) ;
1497
1510
if !g. is_some_and ( |g| print_where_clause_and_check ( w, g, cx) ) {
1498
1511
// If there wasn't a `where` clause, we add a whitespace.
1499
1512
w. write_str ( " " ) ;
@@ -1552,6 +1565,7 @@ fn item_variants(
1552
1565
cx : & mut Context < ' _ > ,
1553
1566
it : & clean:: Item ,
1554
1567
variants : & IndexVec < VariantIdx , clean:: Item > ,
1568
+ enum_def_id : DefId ,
1555
1569
) {
1556
1570
let tcx = cx. tcx ( ) ;
1557
1571
write ! (
@@ -1564,7 +1578,7 @@ fn item_variants(
1564
1578
document_non_exhaustive_header( it) ,
1565
1579
document_non_exhaustive( it)
1566
1580
) ;
1567
- let should_show_enum_discriminant = should_show_enum_discriminant ( variants) ;
1581
+ let should_show_enum_discriminant = should_show_enum_discriminant ( cx , enum_def_id , variants) ;
1568
1582
for ( index, variant) in variants. iter_enumerated ( ) {
1569
1583
if variant. is_stripped ( ) {
1570
1584
continue ;
@@ -1594,7 +1608,7 @@ fn item_variants(
1594
1608
var,
1595
1609
index,
1596
1610
should_show_enum_discriminant,
1597
- it . def_id ( ) . unwrap ( ) ,
1611
+ enum_def_id ,
1598
1612
) ;
1599
1613
} else {
1600
1614
w. write_str ( variant. name . unwrap ( ) . as_str ( ) ) ;
0 commit comments