From 6aaed33efa041052e3e7bb724108dcd8c63ea681 Mon Sep 17 00:00:00 2001 From: binarycat Date: Sun, 20 Oct 2024 11:34:07 -0500 Subject: [PATCH] rustdoc: prefer showing enum variants as written fixes https://github.com/rust-lang/rust/issues/128347 --- src/librustdoc/html/render/print_item.rs | 3 ++- tests/rustdoc/src-discrim.perms.html | 6 ++++++ tests/rustdoc/src-discrim.rs | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/rustdoc/src-discrim.perms.html create mode 100644 tests/rustdoc/src-discrim.rs diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 3c96f8736814e..49967c9e32b2f 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1540,7 +1540,8 @@ fn display_c_like_variant( ) { let name = item.name.unwrap(); if let Some(ref value) = variant.discriminant { - write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true)); + let v = value.expr(cx.tcx()).unwrap_or(value.value(cx.tcx(), true)); + write!(w, "{} = {}", name.as_str(), v); } else if should_show_enum_discriminant { let adt_def = cx.tcx().adt_def(enum_def_id); let discr = adt_def.discriminant_for_variant(cx.tcx(), index); diff --git a/tests/rustdoc/src-discrim.perms.html b/tests/rustdoc/src-discrim.perms.html new file mode 100644 index 0000000000000..ac7c722f97eac --- /dev/null +++ b/tests/rustdoc/src-discrim.perms.html @@ -0,0 +1,6 @@ +
#[repr(u8)]
pub enum Permissions { + Guest = b'%', + User = b'$', + System = b'@', + Absolute = b'#', +}
\ No newline at end of file diff --git a/tests/rustdoc/src-discrim.rs b/tests/rustdoc/src-discrim.rs new file mode 100644 index 0000000000000..347a233f44427 --- /dev/null +++ b/tests/rustdoc/src-discrim.rs @@ -0,0 +1,12 @@ +//! regression test for #128347 +#![crate_name = "foo"] + +//@ snapshot perms foo/enum.Permissions.html '//pre/code' + +#[repr(u8)] +pub enum Permissions { + Guest = b'%', + User = b'$', + System = b'@', + Absolute = b'#', +}