Skip to content

Commit aff4cd5

Browse files
committed
Report Layout of enum variants
Followup of #83501, Fixes #86253.
1 parent ac50a53 commit aff4cd5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/librustdoc/html/render/print_item.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use rustc_data_structures::fx::FxHashMap;
77
use rustc_hir as hir;
88
use rustc_hir::def::CtorKind;
99
use rustc_hir::def_id::DefId;
10+
use rustc_middle::bug;
1011
use rustc_middle::middle::stability;
1112
use rustc_middle::ty::layout::LayoutError;
12-
use rustc_middle::ty::TyCtxt;
13+
use rustc_middle::ty::{Adt, TyCtxt};
1314
use rustc_span::hygiene::MacroKind;
1415
use rustc_span::symbol::{kw, sym, Symbol};
16+
use rustc_target::abi::Variants;
1517

1618
use super::{
1719
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
@@ -1636,6 +1638,38 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16361638
pl = if bytes == 1 { "" } else { "s" },
16371639
);
16381640
}
1641+
if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants {
1642+
if !variants.is_empty() {
1643+
w.write_str(
1644+
"<p>\
1645+
<strong>Size for each variant:</strong>\
1646+
<ul>",
1647+
);
1648+
1649+
let adt = if let Adt(adt, _) = ty_layout.ty.kind() {
1650+
adt
1651+
} else {
1652+
bug!("not an adt")
1653+
};
1654+
1655+
for (index, layout) in variants.iter_enumerated() {
1656+
let ident = adt.variants[index].ident;
1657+
if layout.abi.is_unsized() {
1658+
writeln!(w, "<li><code>{name}</code> (unsized)</li>", name = ident);
1659+
} else {
1660+
let bytes = layout.size.bytes();
1661+
writeln!(
1662+
w,
1663+
"<li><code>{name}</code>: {size} byte{pl}</li>",
1664+
name = ident,
1665+
size = bytes,
1666+
pl = if bytes == 1 { "" } else { "s" },
1667+
);
1668+
}
1669+
}
1670+
w.write_str("</ul></p>");
1671+
}
1672+
}
16391673
}
16401674
// This kind of layout error can occur with valid code, e.g. if you try to
16411675
// get the layout of a generic type such as `Vec<T>`.

src/test/rustdoc/type-layout.rs

+6
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ pub struct Unsized([u8]);
5252

5353
// @!has type_layout/trait.MyTrait.html 'Size: '
5454
pub trait MyTrait {}
55+
56+
// @has type_layout/enum.Variants.html '1 byte'
57+
pub enum Variants {
58+
A,
59+
B(u8),
60+
}

0 commit comments

Comments
 (0)