diff options
| author | Deadbeef <ent3rm4n@gmail.com> | 2021-06-14 01:49:37 +0800 |
|---|---|---|
| committer | Deadbeef <ent3rm4n@gmail.com> | 2021-08-31 05:24:18 +0000 |
| commit | aff4cd5ce725602249c2554b04b1066d09acd31e (patch) | |
| tree | f8915f7a08176769ce67ad2f1f53ab2ffba79114 | |
| parent | ac50a53359328a5d7f2f558833e63d59d372e4f7 (diff) | |
| download | rust-aff4cd5ce725602249c2554b04b1066d09acd31e.tar.gz rust-aff4cd5ce725602249c2554b04b1066d09acd31e.zip | |
Report Layout of enum variants
Followup of #83501, Fixes #86253.
| -rw-r--r-- | src/librustdoc/html/render/print_item.rs | 36 | ||||
| -rw-r--r-- | src/test/rustdoc/type-layout.rs | 6 |
2 files changed, 41 insertions, 1 deletions
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 8f4857a6939..f362a288bcb 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -7,11 +7,13 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; +use rustc_middle::bug; use rustc_middle::middle::stability; use rustc_middle::ty::layout::LayoutError; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{Adt, TyCtxt}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; +use rustc_target::abi::Variants; use super::{ 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) { pl = if bytes == 1 { "" } else { "s" }, ); } + if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants { + if !variants.is_empty() { + w.write_str( + "<p>\ + <strong>Size for each variant:</strong>\ + <ul>", + ); + + let adt = if let Adt(adt, _) = ty_layout.ty.kind() { + adt + } else { + bug!("not an adt") + }; + + for (index, layout) in variants.iter_enumerated() { + let ident = adt.variants[index].ident; + if layout.abi.is_unsized() { + writeln!(w, "<li><code>{name}</code> (unsized)</li>", name = ident); + } else { + let bytes = layout.size.bytes(); + writeln!( + w, + "<li><code>{name}</code>: {size} byte{pl}</li>", + name = ident, + size = bytes, + pl = if bytes == 1 { "" } else { "s" }, + ); + } + } + w.write_str("</ul></p>"); + } + } } // This kind of layout error can occur with valid code, e.g. if you try to // get the layout of a generic type such as `Vec<T>`. diff --git a/src/test/rustdoc/type-layout.rs b/src/test/rustdoc/type-layout.rs index 272911de681..f398dd776d9 100644 --- a/src/test/rustdoc/type-layout.rs +++ b/src/test/rustdoc/type-layout.rs @@ -52,3 +52,9 @@ pub struct Unsized([u8]); // @!has type_layout/trait.MyTrait.html 'Size: ' pub trait MyTrait {} + +// @has type_layout/enum.Variants.html '1 byte' +pub enum Variants { + A, + B(u8), +} |
