@@ -7,11 +7,13 @@ use rustc_data_structures::fx::FxHashMap;
77use rustc_hir as hir;
88use rustc_hir:: def:: CtorKind ;
99use rustc_hir:: def_id:: DefId ;
10+ use rustc_middle:: bug;
1011use rustc_middle:: middle:: stability;
1112use rustc_middle:: ty:: layout:: LayoutError ;
12- use rustc_middle:: ty:: TyCtxt ;
13+ use rustc_middle:: ty:: { Adt , TyCtxt } ;
1314use rustc_span:: hygiene:: MacroKind ;
1415use rustc_span:: symbol:: { kw, sym, Symbol } ;
16+ use rustc_target:: abi:: Variants ;
1517
1618use 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>`.
0 commit comments