Skip to content

Commit 8096910

Browse files
committed
Report variant size without the discriminant
1 parent c349b79 commit 8096910

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::layout::LayoutError;
1313
use rustc_middle::ty::{Adt, TyCtxt};
1414
use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
16-
use rustc_target::abi::{Layout, Variants};
16+
use rustc_target::abi::{Layout, Primitive, Variants};
1717

1818
use super::{
1919
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
@@ -1606,11 +1606,11 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
16061606
}
16071607

16081608
fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1609-
fn write_size_of_layout(w: &mut Buffer, layout: &Layout) {
1609+
fn write_size_of_layout(w: &mut Buffer, layout: &Layout, tag_size: u64) {
16101610
if layout.abi.is_unsized() {
16111611
write!(w, "(unsized)");
16121612
} else {
1613-
let bytes = layout.size.bytes();
1613+
let bytes = layout.size.bytes() - tag_size;
16141614
write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
16151615
}
16161616
}
@@ -1637,9 +1637,9 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16371637
chapter for details on type layout guarantees.</p></div>"
16381638
);
16391639
w.write_str("<p><strong>Size:</strong> ");
1640-
write_size_of_layout(w, ty_layout.layout);
1640+
write_size_of_layout(w, ty_layout.layout, 0);
16411641
writeln!(w, "</p>");
1642-
if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants {
1642+
if let Variants::Multiple { variants, tag, .. } = &ty_layout.layout.variants {
16431643
if !variants.is_empty() {
16441644
w.write_str(
16451645
"<p>\
@@ -1653,10 +1653,16 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16531653
span_bug!(tcx.def_span(ty_def_id), "not an adt")
16541654
};
16551655

1656+
let tag_size = if let Primitive::Int(i, _) = tag.value {
1657+
i.size().bytes()
1658+
} else {
1659+
span_bug!(tcx.def_span(ty_def_id), "tag is not int")
1660+
};
1661+
16561662
for (index, layout) in variants.iter_enumerated() {
16571663
let ident = adt.variants[index].ident;
1658-
write!(w, "<li><code>{name}</code> ", name = ident);
1659-
write_size_of_layout(w, layout);
1664+
write!(w, "<li><code>{name}</code>: ", name = ident);
1665+
write_size_of_layout(w, layout, tag_size);
16601666
writeln!(w, "</li>");
16611667
}
16621668
w.write_str("</ul></p>");

src/test/rustdoc/type-layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub struct Unsized([u8]);
5353
// @!has type_layout/trait.MyTrait.html 'Size: '
5454
pub trait MyTrait {}
5555

56-
// @has type_layout/enum.Variants.html '1 byte'
56+
// @has type_layout/enum.Variants.html '<code>A</code>: 0 bytes'
57+
// @has - '<code>B</code>: 1 byte'
5758
pub enum Variants {
5859
A,
5960
B(u8),

0 commit comments

Comments
 (0)