Skip to content

Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387

Open
glandium wants to merge 1 commit into
rust-lang:mainfrom
glandium:issue3386
Open

Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387
glandium wants to merge 1 commit into
rust-lang:mainfrom
glandium:issue3386

Conversation

@glandium

@glandium glandium commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

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

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
Comment thread bindgen/ir/ty.rs
// 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!(

@emilio emilio Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check the USR tho? Can we check cursor == inner.declaration() or so?

View changes since the review

@glandium glandium Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObjC id/SEL/Class typedef shadowing causes infinite recursion under clang 22

2 participants