Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustdoc: Report Layout of enum variants #86263

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions
  • Loading branch information
fee1-dead committed Aug 31, 2021
commit c349b79029770e39963fadf5021cfa6a6cfe5472
44 changes: 18 additions & 26 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +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::span_bug;
use rustc_middle::ty::layout::LayoutError;
use rustc_middle::ty::{Adt, TyCtxt};
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_target::abi::Variants;
use rustc_target::abi::{Layout, Variants};

use super::{
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
Expand Down Expand Up @@ -1606,6 +1606,15 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
}

fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
fn write_size_of_layout(w: &mut Buffer, layout: &Layout) {
if layout.abi.is_unsized() {
write!(w, "(unsized)");
} else {
let bytes = layout.size.bytes();
write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
}
}

if !cx.shared.show_type_layout {
return;
}
Expand All @@ -1627,17 +1636,9 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
<a href=\"https://doc.rust-lang.org/reference/type-layout.html\">“Type Layout”</a> \
chapter for details on type layout guarantees.</p></div>"
);
if ty_layout.layout.abi.is_unsized() {
writeln!(w, "<p><strong>Size:</strong> (unsized)</p>");
} else {
let bytes = ty_layout.layout.size.bytes();
writeln!(
w,
"<p><strong>Size:</strong> {size} byte{pl}</p>",
size = bytes,
pl = if bytes == 1 { "" } else { "s" },
);
}
w.write_str("<p><strong>Size:</strong> ");
write_size_of_layout(w, ty_layout.layout);
writeln!(w, "</p>");
if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants {
if !variants.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test what happens for the empty case? Does it show 0 bytes or unsized? I think ideally it would show "uninhabited" or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shows 0 bytes. I think it would be more suitable for a separate PR as a followup of the original PR.

w.write_str(
Expand All @@ -1649,23 +1650,14 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
let adt = if let Adt(adt, _) = ty_layout.ty.kind() {
adt
} else {
bug!("not an adt")
span_bug!(tcx.def_span(ty_def_id), "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" },
);
}
write!(w, "<li><code>{name}</code> ", name = ident);
write_size_of_layout(w, layout);
writeln!(w, "</li>");
}
w.write_str("</ul></p>");
}
Expand Down