Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,11 +1656,9 @@ fn display_c_like_variant(
} 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);
if discr.ty.is_signed() {
write!(w, "{} = {}", name.as_str(), discr.val as i128)?;
} else {
write!(w, "{} = {}", name.as_str(), discr.val)?;
}
// Use `discr`'s `Display` impl to render the value with the correct
// signedness, including proper sign-extension for signed types.
write!(w, "{} = {}", name.as_str(), discr)?;
} else {
write!(w, "{name}")?;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/rustdoc/enum/enum-variant-value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,25 @@ pub use bar::P;
//@ has - '//*[@id="variant.A"]/h3' 'A(u32)'
//@ matches - '//*[@id="variant.B"]/h3' '^B$'
pub use bar::Q;

// Ensure signed implicit discriminants are rendered correctly after a negative explicit value.
//@ has 'foo/enum.R.html'
//@ has - '//*[@class="rust item-decl"]/code' 'A = -2,'
//@ has - '//*[@class="rust item-decl"]/code' 'B = -1,'
//@ matches - '//*[@id="variant.A"]/h3' '^A = -2$'
//@ matches - '//*[@id="variant.B"]/h3' '^B = -1$'
pub enum R {
A = -2,
B,
}

// Also check that incrementing -1 yields 0 for the next implicit variant.
//@ has 'foo/enum.S.html'
//@ has - '//*[@class="rust item-decl"]/code' 'A = -1,'
//@ has - '//*[@class="rust item-decl"]/code' 'B = 0,'
//@ matches - '//*[@id="variant.A"]/h3' '^A = -1$'
//@ matches - '//*[@id="variant.B"]/h3' '^B = 0$'
pub enum S {
A = -1,
B,
}
Loading