Skip to content

Commit 3ce7dd2

Browse files
Show enum discriminant if a compatible repr is used
1 parent 1210aac commit 3ce7dd2

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/librustdoc/html/render/print_item.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::def::CtorKind;
77
use rustc_hir::def_id::DefId;
88
use rustc_index::IndexVec;
99
use rustc_middle::middle::stability;
10+
use rustc_middle::query::Key;
1011
use rustc_middle::ty::{self, TyCtxt};
1112
use rustc_span::hygiene::MacroKind;
1213
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
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,20 @@ 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 a value set or if the enum has `#[repr(C)]` or has an integer `repr`.
1451+
fn should_show_enum_discriminant(
1452+
cx: &Context<'_>,
1453+
enum_def_id: DefId,
1454+
variants: &IndexVec<VariantIdx, clean::Item>,
1455+
) -> bool {
14481456
let mut has_variants_with_value = false;
14491457
for variant in variants {
14501458
if let clean::VariantItem(ref var) = *variant.kind &&
@@ -1455,7 +1463,12 @@ fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -
14551463
return false;
14561464
}
14571465
}
1458-
has_variants_with_value
1466+
if has_variants_with_value {
1467+
return true;
1468+
}
1469+
let repr = cx.tcx().adt_def(enum_def_id).repr();
1470+
// let repr = cx.tcx().repr_options_of_def(enum_def_id);
1471+
repr.c() || repr.int.is_some()
14591472
}
14601473

14611474
fn 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

Comments
 (0)