Skip to content

Outdated docs claim that #[repr(transparent)] on empty structs is not permitted #129029

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

Closed
scovich opened this issue Aug 12, 2024 · 6 comments
Closed
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scovich
Copy link

scovich commented Aug 12, 2024

This code compiles:

#[repr(transparent)] struct Foo {}

Going by the docs for #[repr(transparent)]:

#[repr(transparent)] can only be used on a struct or single-variant enum that has a single non-zero-sized field (there may be additional zero-sized fields). The effect is that the layout and ABI of the whole struct/enum is guaranteed to be the same as that one field.

... it seems like the compiler should reject that code, with diagnostics similar to those it emits for an empty transparent enum:

error[E0084]: unsupported representation for zero-variant enum
 --> src/lib.rs:2:1
  |
2 | #[repr(transparent)] enum E {}
  | ^^^^^^^^^^^^^^^^^^^^ ------ zero-variant enum

error[E0731]: transparent enum needs exactly one variant, but has 0
 --> src/lib.rs:2:22
  |
2 | #[repr(transparent)] enum E {}
  |                      ^^^^^^ needs exactly one variant, but has 0

Some errors have detailed explanations: E0084, E0731.
@scovich scovich added the C-bug Category: This is a bug. label Aug 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 12, 2024
@scovich scovich changed the title Compiler does not reject #[repr(transparent)] for empty struct Compiler allows #[repr(transparent)] for empty struct Aug 12, 2024
@compiler-errors
Copy link
Member

This regressed somewhere in 1.55

@compiler-errors
Copy link
Member

This is intentional: #86279

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2024
@compiler-errors
Copy link
Member

Notably, the difference between empty (variantless) enums and empty (fieldless) structs are that the former are uninhabited. Fieldless structs just have 0 size.

@compiler-errors
Copy link
Member

Actually, is the reference out of date? The reference says:

The transparent representation can only be used on a struct or an enum with a single variant that has:
* a single field with non-zero size, and
* any number of fields with size 0 and alignment 1

@compiler-errors
Copy link
Member

We should update the reference anyways to note this change in rules. I still don't consider this to be a bug according to these rules:

#77841 (comment)

@scovich
Copy link
Author

scovich commented Aug 13, 2024

Yes, reading the RFC, there is no bug, but the stale documentation needs to be fixed so people are not misled.

@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 13, 2024
@fmease fmease changed the title Compiler allows #[repr(transparent)] for empty struct Outdated docs claim that #[repr(transparent)] on empty struct is not permitted Aug 13, 2024
@fmease fmease changed the title Outdated docs claim that #[repr(transparent)] on empty struct is not permitted Outdated docs claim that #[repr(transparent)] on empty structs is not permitted Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants