@@ -7,6 +7,7 @@ use rustc_hir as hir;
77use rustc_hir:: def:: CtorKind ;
88use rustc_hir:: def_id:: DefId ;
99use rustc_middle:: middle:: stability;
10+ use rustc_middle:: ty:: layout:: LayoutError ;
1011use rustc_middle:: ty:: TyCtxt ;
1112use rustc_span:: hygiene:: MacroKind ;
1213use rustc_span:: symbol:: { kw, sym, Symbol } ;
@@ -1540,13 +1541,14 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
15401541 return ;
15411542 }
15421543
1544+ writeln ! ( w, "<h2 class=\" small-section-header\" >Layout</h2>" ) ;
1545+ writeln ! ( w, "<div class=\" docblock\" >" ) ;
1546+
15431547 let tcx = cx. tcx ( ) ;
15441548 let param_env = tcx. param_env ( ty_def_id) ;
15451549 let ty = tcx. type_of ( ty_def_id) ;
15461550 match tcx. layout_of ( param_env. and ( ty) ) {
15471551 Ok ( ty_layout) => {
1548- writeln ! ( w, "<h2 class=\" small-section-header\" >Layout</h2>" ) ;
1549- writeln ! ( w, "<div class=\" docblock\" >" ) ;
15501552 writeln ! (
15511553 w,
15521554 "<div class=\" warning\" ><p><strong>Note:</strong> Most layout information is \
@@ -1567,11 +1569,28 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
15671569 pl = if bytes == 1 { "" } else { "s" } ,
15681570 ) ;
15691571 }
1570- writeln ! ( w, "</div>" ) ;
15711572 }
1572- // Layout errors can occur with valid code, e.g. if you try to get the layout
1573- // of a generic type such as `Vec<T>`. In case of a layout error, we just
1574- // don't show any layout information.
1575- Err ( _) => { }
1573+ // This kind of layout error can occur with valid code, e.g. if you try to
1574+ // get the layout of a generic type such as `Vec<T>`.
1575+ Err ( LayoutError :: Unknown ( _) ) => {
1576+ writeln ! (
1577+ w,
1578+ "<p><strong>Note:</strong> Unable to compute type layout, \
1579+ possibly due to this type having generic parameters. \
1580+ Layout can only be computed for concrete, fully-instantiated types.</p>"
1581+ ) ;
1582+ }
1583+ // This kind of error probably can't happen with valid code, but we don't
1584+ // want to panic and prevent the docs from building, so we just let the
1585+ // user know that we couldn't compute the layout.
1586+ Err ( LayoutError :: SizeOverflow ( _) ) => {
1587+ writeln ! (
1588+ w,
1589+ "<p><strong>Note:</strong> Encountered an error during type layout; \
1590+ the type was too big.</p>"
1591+ ) ;
1592+ }
15761593 }
1594+
1595+ writeln ! ( w, "</div>" ) ;
15771596}
0 commit comments