@@ -7,11 +7,13 @@ use rustc_data_structures::fx::FxHashMap;
7
7
use rustc_hir as hir;
8
8
use rustc_hir:: def:: CtorKind ;
9
9
use rustc_hir:: def_id:: DefId ;
10
+ use rustc_middle:: bug;
10
11
use rustc_middle:: middle:: stability;
11
12
use rustc_middle:: ty:: layout:: LayoutError ;
12
- use rustc_middle:: ty:: TyCtxt ;
13
+ use rustc_middle:: ty:: { Adt , TyCtxt } ;
13
14
use rustc_span:: hygiene:: MacroKind ;
14
15
use rustc_span:: symbol:: { kw, sym, Symbol } ;
16
+ use rustc_target:: abi:: Variants ;
15
17
16
18
use super :: {
17
19
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) {
1636
1638
pl = if bytes == 1 { "" } else { "s" } ,
1637
1639
) ;
1638
1640
}
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
+ }
1639
1673
}
1640
1674
// This kind of layout error can occur with valid code, e.g. if you try to
1641
1675
// get the layout of a generic type such as `Vec<T>`.
0 commit comments