Skip to content

Commit a576dfd

Browse files
Show enum variant value if it is a C-like variant
1 parent af68593 commit a576dfd

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,9 +2091,8 @@ impl Discriminant {
20912091
pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> Option<String> {
20922092
self.expr.map(|body| rendered_const(tcx, body))
20932093
}
2094-
/// Will always be a machine readable number, without underscores or suffixes.
2095-
pub(crate) fn value(&self, tcx: TyCtxt<'_>) -> String {
2096-
print_evaluated_const(tcx, self.value, false).unwrap()
2094+
pub(crate) fn value(&self, tcx: TyCtxt<'_>, with_underscores: bool) -> String {
2095+
print_evaluated_const(tcx, self.value, with_underscores, false).unwrap()
20972096
}
20982097
}
20992098

@@ -2348,7 +2347,7 @@ impl ConstantKind {
23482347
match *self {
23492348
ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
23502349
ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
2351-
print_evaluated_const(tcx, def_id, true)
2350+
print_evaluated_const(tcx, def_id, true, true)
23522351
}
23532352
}
23542353
}

src/librustdoc/clean/utils.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
275275
pub(crate) fn print_evaluated_const(
276276
tcx: TyCtxt<'_>,
277277
def_id: DefId,
278-
underscores_and_type: bool,
278+
with_underscores: bool,
279+
with_type: bool,
279280
) -> Option<String> {
280281
tcx.const_eval_poly(def_id).ok().and_then(|val| {
281282
let ty = tcx.type_of(def_id).instantiate_identity();
@@ -284,7 +285,7 @@ pub(crate) fn print_evaluated_const(
284285
(mir::ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
285286
(mir::ConstValue::Scalar(_), _) => {
286287
let const_ = mir::Const::from_value(val, ty);
287-
Some(print_const_with_custom_print_scalar(tcx, const_, underscores_and_type))
288+
Some(print_const_with_custom_print_scalar(tcx, const_, with_underscores, with_type))
288289
}
289290
_ => None,
290291
}
@@ -320,14 +321,25 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
320321
fn print_const_with_custom_print_scalar<'tcx>(
321322
tcx: TyCtxt<'tcx>,
322323
ct: mir::Const<'tcx>,
323-
underscores_and_type: bool,
324+
with_underscores: bool,
325+
with_type: bool,
324326
) -> String {
325327
// Use a slightly different format for integer types which always shows the actual value.
326328
// For all other types, fallback to the original `pretty_print_const`.
327329
match (ct, ct.ty().kind()) {
328330
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
329-
if underscores_and_type {
330-
format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
331+
if with_underscores {
332+
if with_type {
333+
format!(
334+
"{}{}",
335+
format_integer_with_underscore_sep(&int.to_string()),
336+
ui.name_str()
337+
)
338+
} else {
339+
format_integer_with_underscore_sep(&int.to_string())
340+
}
341+
} else if with_type {
342+
format!("{}{}", int.to_string(), ui.name_str())
331343
} else {
332344
int.to_string()
333345
}
@@ -337,12 +349,18 @@ fn print_const_with_custom_print_scalar<'tcx>(
337349
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
338350
let data = int.assert_bits(size);
339351
let sign_extended_data = size.sign_extend(data) as i128;
340-
if underscores_and_type {
341-
format!(
342-
"{}{}",
343-
format_integer_with_underscore_sep(&sign_extended_data.to_string()),
344-
i.name_str()
345-
)
352+
if with_underscores {
353+
if with_type {
354+
format!(
355+
"{}{}",
356+
format_integer_with_underscore_sep(&sign_extended_data.to_string()),
357+
i.name_str()
358+
)
359+
} else {
360+
format_integer_with_underscore_sep(&sign_extended_data.to_string())
361+
}
362+
} else if with_type {
363+
format!("{}{}", sign_extended_data.to_string(), i.name_str())
346364
} else {
347365
sign_extended_data.to_string()
348366
}

src/librustdoc/html/render/print_item.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,9 +1467,15 @@ fn render_enum_fields<'a>(
14671467
match *v.kind {
14681468
// FIXME(#101337): Show discriminant
14691469
clean::VariantItem(ref var) => match var.kind {
1470-
clean::VariantKind::CLike => w.write_str(name.as_str()),
1470+
clean::VariantKind::CLike => {
1471+
if let Some(ref value) = var.discriminant {
1472+
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
1473+
} else {
1474+
w.write_str(name.as_str());
1475+
}
1476+
}
14711477
clean::VariantKind::Tuple(ref s) => {
1472-
write!(w, "{name}({})", print_tuple_struct_fields(cx, s),);
1478+
write!(w, "{name}({})", print_tuple_struct_fields(cx, s));
14731479
}
14741480
clean::VariantKind::Struct(ref s) => {
14751481
render_struct(w, v, None, None, &s.fields, TAB, false, cx);
@@ -1523,6 +1529,12 @@ fn item_variants<'a>(
15231529
" rightside",
15241530
);
15251531
write!(w, "<h3 class=\"code-header\">{name}", name = variant.name.unwrap());
1532+
if let clean::VariantItem(ref var) = *variant.kind &&
1533+
let clean::VariantKind::CLike = var.kind &&
1534+
let Some(ref value) = var.discriminant
1535+
{
1536+
write!(w, " = {}", value.value(cx.tcx(), true));
1537+
}
15261538

15271539
let clean::VariantItem(variant_data) = &*variant.kind else { unreachable!() };
15281540

src/librustdoc/json/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl FromWithTcx<clean::Discriminant> for Discriminant {
745745
// `rustc_middle` types, not `rustc_hir`, but because JSON never inlines
746746
// the expr is always some.
747747
expr: disr.expr(tcx).unwrap(),
748-
value: disr.value(tcx),
748+
value: disr.value(tcx, false),
749749
}
750750
}
751751
}

0 commit comments

Comments
 (0)