Skip to content

repr(transparent): don't consider most length-0 arrays trivial#155984

Draft
Jules-Bertholet wants to merge 3 commits intorust-lang:mainfrom
Jules-Bertholet:nontrivial-array
Draft

repr(transparent): don't consider most length-0 arrays trivial#155984
Jules-Bertholet wants to merge 3 commits intorust-lang:mainfrom
Jules-Bertholet:nontrivial-array

Conversation

@Jules-Bertholet
Copy link
Copy Markdown
Contributor

@Jules-Bertholet Jules-Bertholet commented Apr 30, 2026

With this PR, an array type is considered trivial for the purpose of repr(transparent) only if its element type is—we emit the repr_transparent_non_zst_fields FCW (#78586) otherwise. To support a pattern used by the ghost crate, we also permit all array types with length 0 when they are contained within a repr(Rust, packed(1)) ADT.

This has two benefits:

Forbid non-portable definitions

Some types have alignment 1 only on certain platforms. Prior to this PR, the following snippet would compile on AVR, and only on AVR:

#[repr(transparent)]
struct Foo(i32, [u16; 0]);

After this PR, the above now fails to compile on any target.

FFI and CFI compatibility

We want to add support for Control Flow Integrity to Rust at some point. There are some good reasons to want CFI to consider *const [u8; 0] and *const [u8; 1] compatible with one another. But that means we must consider *const [u8; 0] and *const () to be CFI-incompatible. Declaring [u8; 0] non-trivial for repr(transparent) makes that easier to achieve. See discussion on Zulip:
https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/ABI-compatibility.20rules.20of.20ZST.20types/near/591412488

@rustbot label T-lang needs-fcp A-repr
Also needs a crater run.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 30, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 30, 2026

r? @dingxiangfei2009

rustbot has assigned @dingxiangfei2009.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

@rustbot rustbot added A-repr Area: the `#[repr(stuff)]` attribute needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-lang Relevant to the language team labels Apr 30, 2026
@rust-log-analyzer

This comment has been minimized.

Comment on lines +1808 to +1809
if elem_trivial {
check_unsuited(tcx, typing_env, *elem_ty)
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 30, 2026

Choose a reason for hiding this comment

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

Why do you allow any arrays here? Seems easier to just reject them all?
I'd be surprised if there is much/any use of arrays as "trivial" types in repr(transparent).

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's conceptually nice to treat "array with element type T" the same as "struct with field of type T". And it turns out that we need some non-trivial complexity anyway, to support the ghost crate.

@RalfJung
Copy link
Copy Markdown
Member

Also needs a crater run.

For that we need a version of this that emits a hard error.

@Jules-Bertholet Jules-Bertholet force-pushed the nontrivial-array branch 2 times, most recently from 72a9d17 to d4c13b1 Compare April 30, 2026 23:11
@rust-log-analyzer

This comment has been minimized.

With this PR, an array type is considered trivial
for the purpose of `repr(transparent)` only if its
element type is—we emit the `repr_transparent_non_zst_fields` FCW
otherwise.

This has two benefits:

## Forbid non-portable definitions

Some types have alignment 1 only on certain platforms.
Prior to this PR, the following snippet would compile on AVR,
and *only* on AVR:

```rust
#[repr(transparent)]
struct Foo(i32, [u16; 0]);
```

After this PR, the above now fails to compile on any target.

## FFI and CFI compatibility

We want to add support for Control Flow Integrity to Rust at some point.
There are some good reasons to want CFI to consider `*const [u8; 0]`
and `*const [u8; 1]` compatible with one another. But that means we must
consider `*const [u8; 0]` and `*const ()` to be CFI-incompatible.
Declaring `[u8; 0]` non-trivial for `repr(transparent)` makes that
easier to achieve. See discussion on Zulip:
<https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/ABI-compatibility.20rules.20of.20ZST.20types/near/591412488>
Needed to support the `ghost` crate.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 1, 2026

The Miri subtree was changed

cc @rust-lang/miri

@Jules-Bertholet Jules-Bertholet marked this pull request as draft May 1, 2026 02:56
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 1, 2026
@Jules-Bertholet
Copy link
Copy Markdown
Contributor Author

Jules-Bertholet commented May 1, 2026

The PR should now be ready for crater

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-repr Area: the `#[repr(stuff)]` attribute needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants