Skip to content

Add repr(align) to over-aligned empty structs - #3555

Open
samiarif02 wants to merge 1 commit into
rust-lang:mainfrom
samiarif02:empty-struct-align
Open

samiarif02 wants to merge 1 commit into
rust-lang:mainfrom
samiarif02:empty-struct-align

Conversation

@samiarif02

Copy link
Copy Markdown

Fixes #3549.

An empty struct marked __attribute__((aligned(N))) goes through the zero-sized branch in CompInfo::codegen, and that branch never sets explicit_align. So the struct came out as plain #[repr(C)] with alignment 1, its own layout test failed, and so did the layout of any struct that contains it (the task example in the issue).

The fix sets explicit_align for zero-sized, non-union, non-forward-declared structs when the layout asks for more than 1-byte alignment. Ordinary empty structs (align 1) are unchanged.

Tests: added empty-struct-aligned.h (C, size 0) and empty-struct-aligned-cpp.hpp (C++, size 64 with the _address byte). Both expectations differ from main only by the new #[repr(align(64))]. Without the fix the generated layout tests don't compile (align_of 1 vs 64); with it they do. cargo test -p bindgen-tests gives the same results as main apart from the two new tests. Three tests (issue-544-stylo-creduce-2, nsbasehashtable, objc_template) fail locally on main too with Apple's libclang. Changelog entry added under Unreleased/Fixed.

An empty struct with __attribute__((aligned(N))) went through the
zero-sized path in codegen, which never set an explicit alignment, so
the generated struct had alignment 1 and its own layout test failed, as
did the layout of any struct containing it.

Fixes rust-lang#3549.
@rustbot rustbot added the A-C++ label Nov 20, 2026
@samiarif02

Copy link
Copy Markdown
Author

The clippy failure is in bindgen/ir/comp.rs and bindgen/lib.rs, which this PR doesn't touch. It's the new needless_bool lint in current stable clippy, and I get the same two errors on main (0d213bc).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty struct with __attribute__((aligned(N))) is emitted without #[repr(align(N))]

2 participants