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

print-type-sizes: skip field printing for primitives #89329

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,8 +1805,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {

match layout.variants {
Variants::Single { index } => {
debug!("print-type-size `{:#?}` variant {}", layout, adt_def.variants[index].ident);
if !adt_def.variants.is_empty() {
if !adt_def.variants.is_empty() && layout.fields != FieldsShape::Primitive {
Copy link
Member

Choose a reason for hiding this comment

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

Why is variants non-empty here? Primitives aren't enums.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FieldsShape describes how fields are located in memory, with Primitive used for things that have no fields, including the never type.

Variants that are uninhabited and have ZST fields only are consider to be absent. Layout computation omits absents variants entirely. If all variants of an enum are absent, the layout of a never type is used (Variants::Single, FieldsShape::Primitive).

debug!(
"print-type-size `{:#?}` variant {}",
layout, adt_def.variants[index].ident
);
let variant_def = &adt_def.variants[index];
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.ident.name).collect();
record(
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/print_type_sizes/uninhabited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
fn start(_: isize, _: *const *const u8) -> isize {
let _x: Option<!> = None;
let _y: Result<u32, !> = Ok(42);
0
let _z: Result<!, !> = loop {};
}
1 change: 1 addition & 0 deletions src/test/ui/print_type_sizes/uninhabited.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ print-type-size variant `Ok`: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `std::option::Option<!>`: 0 bytes, alignment: 1 bytes
print-type-size variant `None`: 0 bytes
print-type-size type: `std::result::Result<!, !>`: 0 bytes, alignment: 1 bytes