Skip to content

Commit 8b9298b

Browse files
committed
Add note to docs when layout cannot be computed
This should prevent confusion about why generic types don't have layout docs.
1 parent 879a914 commit 8b9298b

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def::CtorKind;
88
use rustc_hir::def_id::DefId;
99
use rustc_middle::middle::stability;
10+
use rustc_middle::ty::layout::LayoutError;
1011
use rustc_middle::ty::TyCtxt;
1112
use rustc_span::hygiene::MacroKind;
1213
use 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
}

src/test/rustdoc/type-layout.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ pub struct Y(u8);
3535
// @has - '0 bytes'
3636
pub struct Z;
3737

38-
// @!has type_layout/struct.Generic.html 'Size: '
38+
// We can't compute layout for generic types.
39+
// @has type_layout/struct.Generic.html 'Unable to compute type layout, possibly due to this type having generic parameters'
40+
// @!has - 'Size: '
3941
pub struct Generic<T>(T);
4042

43+
// We *can*, however, compute layout for types that are only generic over lifetimes,
44+
// because lifetimes are a type-system construct.
45+
// @has type_layout/struct.GenericLifetimes.html 'Size: '
46+
// @has - ' bytes'
47+
pub struct GenericLifetimes<'a>(&'a str);
48+
4149
// @has type_layout/struct.Unsized.html 'Size: '
4250
// @has - '(unsized)'
4351
pub struct Unsized([u8]);

0 commit comments

Comments
 (0)