Skip to content

Fix considering [u8; 0] to have trivial ABI in layout - #166232

Open
beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:fix-trivial-abi-layout
Open

beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:fix-trivial-abi-layout

Conversation

@beetrees

@beetrees beetrees commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any repr(C) types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like [u8; 0] to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in #164628. Split out from #166170.

r? @bjorn3

@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 Nov 20, 2026
@rust-log-analyzer

This comment has been minimized.

@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from 329f4c1 to 90d6dd8 Compare November 20, 2026 03:18
@RalfJung

Copy link
Copy Markdown
Member

This means that the current ABI code considers types like [u8; 0] to be trivial even though they are not - this PR fixes it to match the documentation.

So this is basically fixing a bug in #156112. @Jules-Bertholet does this look reasonable?

@beetrees could you add a test for the case that was buggy?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

but it is important in other places

Which ones?

does this look reasonable?

Yes. It's not clear to me what this fixes in practice, but this PR's logic is a lot more clear/consistent/future-proof than my version, which is reason enough to merge it.

@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from 90d6dd8 to cc109c8 Compare November 20, 2026 18:51
@beetrees

Copy link
Copy Markdown
Contributor Author

Which ones?

#164628 is fixing the 64-bit MIPS ABI. The ABI treats struct { float x; } and struct { float x; char y[0]; } differently, so the ABI needs to handle that while still ensuring that things like PhantomData don't affect the ABI. While Rust doesn't make any guarantees in this area yet, using whether a type has trivial ABI or not to decide whether it affects the ABI is the obvious choice.

@beetrees could you add a test for the case that was buggy?

I've added a rustc_dump_layout to c-zst.rs. I've also addressed #166170 (comment) as it applies to the changes that ended up in this PR.

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

OK, so this doesn't fix any ABI bugs directly, but it's a necessary prerequisite to the fixes in #164628. That makes sense, thanks 👍

Comment thread compiler/rustc_abi/src/lib.rs Resolved
Comment thread compiler/rustc_abi/src/lib.rs Resolved
Comment thread compiler/rustc_abi/src/layout.rs Resolved
@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from cc109c8 to 35832da Compare November 20, 2026 22:25
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Layout invariants are being changed

cc @RalfJung

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from 35832da to 697baa3 Compare November 21, 2026 10:27
Comment thread compiler/rustc_abi/src/lib.rs Resolved
@rust-bors

This comment has been minimized.

@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from 3cb376f to 20965a7 Compare November 27, 2026 07:37
@rustbot

This comment has been minimized.

@bjorn3 bjorn3 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The actual changes LGTM. I haven't digged into the reasoning behind this change though. If someone else has and agrees this should be done, feel free to r+, otherwise I will look at the reasoning later.

View changes since this review

@RalfJung

Copy link
Copy Markdown
Member

[u8; 0] should indeed not have trivial ABI, if that's what you mean? Arrays have trivial ABI only if the field type has trivial ABI; a length of 0 does not change that.

@RalfJung

Copy link
Copy Markdown
Member

It would probably be good to have a codegen-llvm test for whatever ABI this actually affects.

@beetrees
beetrees force-pushed the fix-trivial-abi-layout branch from 20965a7 to c4e8191 Compare November 28, 2026 10:55
@rustbot

rustbot commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@bjorn3

bjorn3 commented Nov 28, 2026

Copy link
Copy Markdown
Member

[u8; 0] should indeed not have trivial ABI, if that's what you mean?

Yes

@bors r+

@rust-bors

rust-bors Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c4e8191 has been tentatively approved by bjorn3

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 28, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 28, 2026
…=bjorn3

Fix considering `[u8; 0]` to have trivial ABI in layout

The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any `repr(C)` types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like `[u8; 0]` to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in rust-lang#164628. Split out from rust-lang#166170.

r? @bjorn3
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 28, 2026
…=bjorn3

Fix considering `[u8; 0]` to have trivial ABI in layout

The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any `repr(C)` types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like `[u8; 0]` to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in rust-lang#164628. Split out from rust-lang#166170.

r? @bjorn3
rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #166232 (Fix considering `[u8; 0]` to have trivial ABI in layout)
 - #166663 (optimize `ArrayWindows::nth`)
 - #166693 (Make function signatures a separate type in `rustc_codegen_llvm`)
 - #166699 (rustc_serialize: Remove unnecessary lifetime from `FileEncoder`)
 - #166665 (Mention alignment in handle_alloc_error)
@rust-bors

rust-bors Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
Fix considering `[u8; 0]` to have trivial ABI in layout



The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any `repr(C)` types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like `[u8; 0]` to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in #164628. Split out from #166170.

r? @bjorn3
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 28, 2026
…=bjorn3

Fix considering `[u8; 0]` to have trivial ABI in layout

The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any `repr(C)` types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like `[u8; 0]` to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in rust-lang#164628. Split out from rust-lang#166170.

r? @bjorn3
@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors yield
Yielding to enclosing rollup

@rust-bors

rust-bors Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #166709.

rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #166232 (Fix considering `[u8; 0]` to have trivial ABI in layout)
 - #166663 (optimize `ArrayWindows::nth`)
 - #166693 (Make function signatures a separate type in `rustc_codegen_llvm`)
 - #166699 (rustc_serialize: Remove unnecessary lifetime from `FileEncoder`)
 - #166366 (Add tests to check we don't confuse LLVM and builtin intrinsics.)
@rust-bors

rust-bors Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
Fix considering `[u8; 0]` to have trivial ABI in layout



The ABI layout handling of trivial ABI currently only checks that the type is a 1-ZST and doesn't contain any `repr(C)` types: however the documentation for trivial ABIs states that any array with a non-trivial element type is itself non-trivial. This means that the current ABI code considers types like `[u8; 0]` to be trivial even though they are not - this PR fixes it to match the documentation. This isn't particularly important for arguments/returns as C doesn't allow plain arrays in the argument/return positions, but it is important in other places. Found by @chenx97 in #164628. Split out from #166170.

r? @bjorn3
@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors yield
Yielding to enclosing rollup

@rust-bors

rust-bors Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #166709.

rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #166232 (Fix considering `[u8; 0]` to have trivial ABI in layout)
 - #166663 (optimize `ArrayWindows::nth`)
 - #166693 (Make function signatures a separate type in `rustc_codegen_llvm`)
 - #166699 (rustc_serialize: Remove unnecessary lifetime from `FileEncoder`)
 - #166366 (Add tests to check we don't confuse LLVM and builtin intrinsics.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants