Skip to content

Wrong diagnostic for zero-sized array in transparent struct #98064

Closed
@lukas-code

Description

@lukas-code

Given the following code:

#[repr(transparent)]
pub struct Aligned<T, A> {
    align: [A; 0],
    value: T,
}

The current output is:

error[E0690]: transparent struct needs at most one non-zero-sized field, but has 2
 --> kernel/src/align.rs:2:1
  |
2 | pub struct Aligned<T, A> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^ needs at most one non-zero-sized field, but has 2
3 |     align: [A; 0],
  |     ------------- this field is non-zero-sized
4 |     value: T,
  |     -------- this field is non-zero-sized

For more information about this error, try `rustc --explain E0690`.

Ideally the output should look like:

error[E0691]: zero-sized field in transparent struct has alignment larger than 1
 --> kernel/src/align.rs:3:5
  |
3 |     align: [A; 0],
  |     ^^^^^^^^^^^^^ may have alignment larger than 1
  |
help: consider changing `#[repr(transparent)]` to `#[repr(C)]`
  |
1 - #[repr(transparent)]
1 + #[repr(C)]
  | 

For more information about this error, try `rustc --explain E0691`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions