ir: Skip phantom anonymous members from inside attribute expressions#3361
Closed
barry3406 wants to merge 1 commit intorust-lang:mainfrom
Closed
ir: Skip phantom anonymous members from inside attribute expressions#3361barry3406 wants to merge 1 commit intorust-lang:mainfrom
barry3406 wants to merge 1 commit intorust-lang:mainfrom
Conversation
Clang exposes anonymous structs declared inside the parent struct's
attribute expressions (e.g. `__alignof__(struct{int aaa;})` inside
`__attribute__((aligned(...)))`) as child cursors of the surrounding
struct, with the surrounding struct as both their lexical and semantic
parent. The existing `is_anonymous() && kind != EnumDecl` check in
`CompInfo::from_ty` happily promoted them to anonymous members, doubling
the surrounding struct's layout and producing failing size assertions
in the generated bindings.
Tell those apart from real anonymous members by comparing source
ranges: a real anonymous member's source range is contained within the
parent struct's source range, while a phantom child from inside an
attribute expression sits past the parent's closing `}`.
Fixes rust-lang#3295
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Clang exposes anonymous structs declared inside the parent struct's attribute expressions (e.g.
__alignof__(struct{int aaa;})inside__attribute__((aligned(...)))) as child cursors of the surrounding struct, with the surrounding struct as both their lexical and semantic parent. The existingis_anonymous() && kind != EnumDeclcheck inCompInfo::from_tyhappily promoted them to anonymous members, so the surrounding struct ended up with an extra phantom field. That doubled the surrounding struct's size, which then made the size assertions in the generated bindings fail to compile (["Size of base_t__bindgen_ty_1"][size_of::<...>() - 4usize]evaluates to an out-of-bounds index).Tell those apart from real anonymous members by comparing source ranges: a real anonymous member's source range is contained inside the parent struct's body, while a phantom child from inside an attribute expression sits past the parent's closing
}. Added a newCursor::extent_containshelper for the check, and gated the anonymous-member promotion on it.Verified against the reproducer from #3295 — the regenerated bindings now have a single
aaa: c_intfield onbase_t__bindgen_ty_1, and the size/offset assertions evaluate cleanly. Added a regression test inbindgen-tests/tests/{headers,expectations/tests}covering exactly the snippet from the issue.Fixes #3295