Change:
introduced a basic section on layout information, with only one piece of information: the total size of the type. I think one useful but easy extension to this is reporting the size of each variant for an enum. cc The "Layout" section is rendered in The variable is a .In that case we have access to an . The part will give us the size of the variant To get the name of the variant, we'll need the stored in , which we can hopefully get from . <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
I think we could just look up the to find the name (there's probably a function for that). Also, it might be worth showing the sizes of each union field.
claim

Discussion:
use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; use rustc_middle::middle::stability; <ins> use rustc_middle::span_bug; </ins> use rustc_middle::ty::layout::LayoutError; <del> use rustc_middle::ty::TyCtxt; </del> <ins> use rustc_middle::ty::{Adt, TyCtxt}; </ins> use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; <ins> use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants}; </ins> use super::{ collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,

} fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) { <ins> fn write_size_of_layout(w: &mut Buffer, layout: &Layout, tag_size: u64) { if layout.abi.is_unsized() { write!(w, "(unsized)"); } else { let bytes = layout.size.bytes() - tag_size; write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },); } } </ins> if !cx.shared.show_type_layout { return; }

<a href="https://doc.rust-lang.org/reference/type-layout.html">“Type Layout”</a>  chapter for details on type layout guarantees.</p></div>" ); <del> if ty_layout.layout.abi.is_unsized() { writeln!(w, "<p><strong>Size:</strong> (unsized)</p>"); } else { let bytes = ty_layout.layout.size.bytes(); writeln!( w, "<p><strong>Size:</strong> {size} byte{pl}</p>", size = bytes, pl = if bytes == 1 { "" } else { "s" }, ); </del> <ins> w.write_str("<p><strong>Size:</strong> "); write_size_of_layout(w, ty_layout.layout, 0); writeln!(w, "</p>"); if let Variants::Multiple { variants, tag, tag_encoding, .. } = &ty_layout.layout.variants { if !variants.is_empty() { w.write_str( "<p><strong>Size for each variant:</strong></p> <ul>", ); let adt = if let Adt(adt, _) = ty_layout.ty.kind() { adt } else { span_bug!(tcx.def_span(ty_def_id), "not an adt") }; let tag_size = if let TagEncoding::Niche { .. } = tag_encoding { 0 } else if let Primitive::Int(i, _) = tag.value { i.size().bytes() } else { span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int") }; for (index, layout) in variants.iter_enumerated() { let ident = adt.variants[index].ident; write!(w, "<li><code>{name}</code>: ", name = ident); write_size_of_layout(w, layout, tag_size); writeln!(w, "</li>"); } w.write_str("</ul>"); } </ins> } } // This kind of layout error can occur with valid code, e.g. if you try to

// @!has type_layout/trait.MyTrait.html 'Size: ' pub trait MyTrait {} <ins> // @has type_layout/enum.Variants.html 'Size: ' // @has - '2 bytes' // @has - '<code>A</code>: 0 bytes' // @has - '<code>B</code>: 1 byte' pub enum Variants { A, B(u8), } // @has type_layout/enum.WithNiche.html 'Size: ' // @has - //p '4 bytes' // @has - '<code>None</code>: 0 bytes' // @has - '<code>Some</code>: 4 bytes' pub enum WithNiche { None, Some(std::num::NonZeroU32), } </ins>