Attempt to implement support for self-referential union types - #3419
Open
erickt wants to merge 2 commits into
Open
Attempt to implement support for self-referential union types#3419erickt wants to merge 2 commits into
erickt wants to merge 2 commits into
Conversation
Update safe_resolve_type to use .and_then(|t| t.kind().as_type()) and convert panicking resolve_type calls across ty.rs and enum_ty.rs to safe_resolve_type. This prevents compiler panics when item IDs are temporarily loaned out during AST graph traversal (with_loaned_item) or when IDs point to non-type items. Test: cargo test -p bindgen-tests TAG=agy CONV=49e4437e-d358-4213-bb5a-8478173dda33
Early return None in CompInfo::layout for uninstantiated C++ template definitions (!self.template_params.is_empty()), avoiding infinite layout recursion on variadic template unions. In CompInfo::is_rust_union, check ctx.uses_any_template_parameters during codegen, emitting struct + __BindgenUnionField<T> wrappers for unconstrained generic template union fields to prevent Rust compiler E0740 errors. Test: cargo test -p bindgen-tests TAG=agy CONV=49e4437e-d358-4213-bb5a-8478173dda33
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.Fixes #3397