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

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

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166140) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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-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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants