@@ -7,6 +7,7 @@ use rustc_hir::def::CtorKind;
77use rustc_hir:: def_id:: DefId ;
88use rustc_index:: IndexVec ;
99use rustc_middle:: middle:: stability;
10+ use rustc_middle:: query:: Key ;
1011use rustc_middle:: ty:: { self , TyCtxt } ;
1112use rustc_span:: hygiene:: MacroKind ;
1213use 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
12491250 match inner_type {
12501251 clean:: TypeAliasInnerType :: Enum { variants, is_non_exhaustive } => {
12511252 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+
12521256 wrap_item ( w, |w| {
12531257 let variants_len = variants. len ( ) ;
12541258 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
12631267 variants_count,
12641268 has_stripped_entries,
12651269 * is_non_exhaustive,
1266- it . def_id ( ) . unwrap ( ) ,
1270+ enum_def_id ,
12671271 )
12681272 } ) ;
1269- item_variants ( w, cx, it, & variants) ;
1273+ item_variants ( w, cx, it, & variants, enum_def_id ) ;
12701274 }
12711275 clean:: TypeAliasInnerType :: Union { fields } => {
12721276 wrap_item ( w, |w| {
@@ -1435,16 +1439,21 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
14351439 write ! ( w, "{}" , document( cx, it, None , HeadingOffset :: H2 ) ) ;
14361440
14371441 if count_variants != 0 {
1438- item_variants ( w, cx, it, & e. variants ) ;
1442+ item_variants ( w, cx, it, & e. variants , it . def_id ( ) . unwrap ( ) ) ;
14391443 }
14401444 let def_id = it. item_id . expect_def_id ( ) ;
14411445 write ! ( w, "{}" , render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ) ;
14421446 write ! ( w, "{}" , document_type_layout( cx, def_id) ) ;
14431447}
14441448
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 {
14481457 let mut has_variants_with_value = false ;
14491458 for variant in variants {
14501459 if let clean:: VariantItem ( ref var) = * variant. kind &&
@@ -1455,7 +1464,11 @@ fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -
14551464 return false ;
14561465 }
14571466 }
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 ( )
14591472}
14601473
14611474fn display_c_like_variant (
@@ -1493,7 +1506,7 @@ fn render_enum_fields(
14931506 is_non_exhaustive : bool ,
14941507 enum_def_id : DefId ,
14951508) {
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) ;
14971510 if !g. is_some_and ( |g| print_where_clause_and_check ( w, g, cx) ) {
14981511 // If there wasn't a `where` clause, we add a whitespace.
14991512 w. write_str ( " " ) ;
@@ -1552,6 +1565,7 @@ fn item_variants(
15521565 cx : & mut Context < ' _ > ,
15531566 it : & clean:: Item ,
15541567 variants : & IndexVec < VariantIdx , clean:: Item > ,
1568+ enum_def_id : DefId ,
15551569) {
15561570 let tcx = cx. tcx ( ) ;
15571571 write ! (
@@ -1564,7 +1578,7 @@ fn item_variants(
15641578 document_non_exhaustive_header( it) ,
15651579 document_non_exhaustive( it)
15661580 ) ;
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) ;
15681582 for ( index, variant) in variants. iter_enumerated ( ) {
15691583 if variant. is_stripped ( ) {
15701584 continue ;
@@ -1594,7 +1608,7 @@ fn item_variants(
15941608 var,
15951609 index,
15961610 should_show_enum_discriminant,
1597- it . def_id ( ) . unwrap ( ) ,
1611+ enum_def_id ,
15981612 ) ;
15991613 } else {
16001614 w. write_str ( variant. name . unwrap ( ) . as_str ( ) ) ;
0 commit comments