Report variant size without the discriminant

This commit is contained in:
Deadbeef 2021-07-10 21:35:40 +08:00
parent c349b79029
commit 8096910b54
No known key found for this signature in database
GPG Key ID: 027DF9338862ADDD
2 changed files with 15 additions and 8 deletions

View File

@ -13,7 +13,7 @@ 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::{Layout, Variants};
use rustc_target::abi::{Layout, Primitive, Variants};
use super::{
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
@ -1606,11 +1606,11 @@ 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) {
fn write_size_of_layout(w: &mut Buffer, layout: &Layout, tag_size: u64) {
if layout.abi.is_unsized() {
write!(w, "(unsized)");
} else {
let bytes = layout.size.bytes();
let bytes = layout.size.bytes() - tag_size;
write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
}
}
@ -1637,9 +1637,9 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
chapter for details on type layout guarantees.</p></div>"
);
w.write_str("<p><strong>Size:</strong> ");
write_size_of_layout(w, ty_layout.layout);
write_size_of_layout(w, ty_layout.layout, 0);
writeln!(w, "</p>");
if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants {
if let Variants::Multiple { variants, tag, .. } = &ty_layout.layout.variants {
if !variants.is_empty() {
w.write_str(
"<p>\
@ -1653,10 +1653,16 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
span_bug!(tcx.def_span(ty_def_id), "not an adt")
};
let tag_size = if let Primitive::Int(i, _) = tag.value {
i.size().bytes()
} else {
span_bug!(tcx.def_span(ty_def_id), "tag is not int")
};
for (index, layout) in variants.iter_enumerated() {
let ident = adt.variants[index].ident;
write!(w, "<li><code>{name}</code> ", name = ident);
write_size_of_layout(w, layout);
write!(w, "<li><code>{name}</code>: ", name = ident);
write_size_of_layout(w, layout, tag_size);
writeln!(w, "</li>");
}
w.write_str("</ul></p>");

View File

@ -53,7 +53,8 @@ pub struct Unsized([u8]);
// @!has type_layout/trait.MyTrait.html 'Size: '
pub trait MyTrait {}
// @has type_layout/enum.Variants.html '1 byte'
// @has type_layout/enum.Variants.html '<code>A</code>: 0 bytes'
// @has - '<code>B</code>: 1 byte'
pub enum Variants {
A,
B(u8),