Attempt to implement support for self-referential union types - #3419
Open
erickt wants to merge 1 commit into
Open
Attempt to implement support for self-referential union types#3419erickt wants to merge 1 commit into
erickt wants to merge 1 commit into
Conversation
Contributor
Author
|
r? @emilio |
erickt
force-pushed
the
union2
branch
8 times, most recently
from
August 4, 2026 22:22
cff08ee to
d21afc5
Compare
In llvm/llvm-project#185449, LLVM's libc++ changed a header to use a recursive self-referential type, which looks approximately like: ``` template <class A0, class... As> union RUnion { A0 arg; RUnion<As...> u; }; template <class A> union RUnion<A> { A arg; }; struct Wrap { RUnion<int, float> u; }; ``` This code caused bindgen to panic. The problem seems to be that bindgen can't handle the recursive self-referential union. Digging into the code, it seems that when this type is being parsed, `with_loaned_item` removes the type from the context. Later on, when parsing the CompKind::Union, CompInfo::layout would call resolve_type on an item that was loaned out, so it panics. This bug was filed in rust-lang#3397. This patch avoids that by first keeping track of which items we have loaed out with `with_loaned_item`, then changing `safe_resolve_type` to panic if we don't have an entry for that type, or it has been loaned out. Then we've updated the call sites to use it. This also adds LLVM-21 tests, since clang generates different code than LLVM-20. Test: cargo test -p bindgen-tests TAG=agy CONV=619bdf72-4d2d-494f-8cb0-f61a6db9674c
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.
In llvm/llvm-project#185449, LLVM's libc++ changed a header to use a recursive self-referential type, which looks approximately like:
This code caused bindgen to panic. The problem seems to be that bindgen can't handle the recursive self-referential union. Digging into the code, it seems that when this type is being parsed,
with_loaned_itemremoves the type from the context. Later on, when parsing theCompKind::Union,CompInfo::layoutwould callresolve_typeon an item that was loaned out, so it panics. This bug was filed in #3397.I've attempted to fix this bug with the help of Gemini agent, and it seems that we might be able to swap out
resolve_typewithsafe_resolve_typein a few locations to get it to stop erroring out. This seems to make sense as best as I understand this situation, although there may be a chance that returningNonein these callsites might be incorrect. But I couldn't find a counter example that shows incorrect code. As a safeguard, this also tracks which items we have loaned out, and makes sure thatsafe_resolve_typewill panic if these types weren't loaned out.Fixes #3397