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 #86253

Closed
LeSeulArtichaut opened this issue Jun 12, 2021 · 2 comments · Fixed by #86263
Closed

Rustdoc: report layout of enum variants #86253

LeSeulArtichaut opened this issue Jun 12, 2021 · 2 comments · Fixed by #86263
Assignees
Labels
A-rustdoc-type-layout Area: `rustdoc --show-type-layout` (nightly-only) C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@LeSeulArtichaut
Copy link
Contributor

LeSeulArtichaut commented Jun 12, 2021

#83501 introduced a basic section on layout information, with only one piece of information: the total size of the type. I think one useful but easy extension to this is reporting the size of each variant for an enum.

cc @camelid

Implementation hints

The "Layout" section is rendered in

Ok(ty_layout) => {
writeln!(
w,
"<div class=\"warning\"><p><strong>Note:</strong> Most layout information is \
completely unstable and may be different between compiler versions and platforms. \
The only exception is types with certain <code>repr(...)</code> attributes. \
Please see the Rust Reference’s \
<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" },
);
}
}

The ty_layout variable is a TyAndLayout. We want to handle the case where ty_layout.layout.variants is Variants::Multiple.In that case we have access to an IndexVec<VariantIdx, Layout>.

  • The Layout part will give us the size of the variant
  • To get the name of the variant, we'll need the AdtDef stored in TyKind::Adt, which we can hopefully get from ty_layout.ty.kind.
@LeSeulArtichaut LeSeulArtichaut added T-rustdoc C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Jun 12, 2021
@camelid
Copy link
Member

camelid commented Jun 12, 2021

To get the name of the variant, we'll need the AdtDef stored in TyKind::Adt, which we can hopefully get from ty_layout.ty.kind.

I think we could just look up the VariantIdx to find the name (there's probably a TyS function for that).

Also, it might be worth showing the sizes of each union field.

@fee1-dead
Copy link
Member

@rustbot claim

@jyn514 jyn514 added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed T-rustdoc-temp labels Jun 22, 2021
@camelid camelid added the A-rustdoc-type-layout Area: `rustdoc --show-type-layout` (nightly-only) label Jul 9, 2021
jackh726 added a commit to jackh726/rust that referenced this issue Sep 8, 2021
…r=camelid

Rustdoc: Report Layout of enum variants

Followup of rust-lang#83501, Fixes rust-lang#86253.

cc `@camelid`

`@rustbot` label A-rustdoc
@bors bors closed this as completed in aff4cd5 Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-type-layout Area: `rustdoc --show-type-layout` (nightly-only) C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants