Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387
Open
glandium wants to merge 1 commit into
Open
Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387glandium wants to merge 1 commit into
glandium wants to merge 1 commit into
Conversation
Objective-C's id/SEL/Class are compiler builtins, but SDK headers also redeclare them via a same-named typedef. Under clang 22, asking such a typedef for its underlying type reports back the same declaration instead of desugaring to the written-out type. Treating that as an ordinary Alias creates a cycle once resolve_typerefs resolves the deferred reference, and nothing that computes canonical types checks for cycles, so it recurses forever - a stack overflow, or an infinite loop if the recursion gets tail-call-optimized. Detect the same-USR case in CXType_Typedef handling and resolve directly as the inner kind instead of aliasing to it, so the cycle is never constructed. Adds a self-contained regression test (the typedef lives in an auxiliary header included via -isystem, so it doesn't need a real macOS SDK) that segfaults on main and passes with this fix. Fixes rust-lang#3386
emilio
reviewed
Jul 3, 2026
| // resolve directly as the inner kind instead of | ||
| // aliasing to it, exactly as we would if `ty` itself had | ||
| // reported that kind. | ||
| let same_underlying_decl = matches!( |
Contributor
There was a problem hiding this comment.
Do we need to check the USR tho? Can we check cursor == inner.declaration() or so?
Contributor
Author
There was a problem hiding this comment.
Unfortunately, cursor == inner.declaration() is false for id/Class even in the exact case we want to catch, and cursor.canonical() == inner.declaration().canonical() is also false. Only the USR matches. Looks like the "builtin definitions" cursor libclang hands back for the inner type isn't the same node as the source-level typedef decl, just the same USR.
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.
Objective-C's id/SEL/Class are compiler builtins, but SDK headers also redeclare them via a same-named typedef.
Under clang 22, asking such a typedef for its underlying type reports back the same declaration instead of desugaring to the written-out type. Treating that as an ordinary Alias creates a cycle once resolve_typerefs resolves the deferred reference, and nothing that computes canonical types checks for cycles, so it recurses forever - a stack overflow, or an infinite loop if the recursion gets tail-call-optimized.
Detect the same-USR case in CXType_Typedef handling and resolve directly as the inner kind instead of aliasing to it, so the cycle is never constructed.
Adds a self-contained regression test (the typedef lives in an auxiliary header included via -isystem, so it doesn't need a real macOS SDK) that segfaults on main and passes with this fix.
Fixes #3386