Skip to content

objc: Don't panic on Objective-C methods returning Class - #3526

Merged
emilio merged 1 commit into
rust-lang:mainfrom
jadenfix:objc-selector-idents
Oct 27, 2026
Merged

emilio merged 1 commit into
rust-lang:mainfrom
jadenfix:objc-selector-idents

Conversation

@jadenfix

Copy link
Copy Markdown
Contributor

Fixes #3484.

CXType_ObjCClass shared a match arm with CXType_ObjCInterface, so the builtin Class type went through ObjCInterface::from_ty on whatever cursor it was seen at. For a method return type that is the method cursor, and the invented interface is named after the selector:

- (Class)take:(int)t;
panicked at bindgen/ir/context.rs:903:9:
"take:" is not a valid Ident

- (Class)cls did not panic but generated an unrelated struct cls(pub id) interface, and a Class parameter did the same with the parameter name.

This adds TypeKind::ObjCClass, handled like ObjCId in the analyses and generated as *mut objc::runtime::Class, which matches how SEL already maps to objc::runtime::Sel.

The only existing expectation that changes is objc_builtin_typedef_cycle: the isa field is now *mut objc::runtime::Class and the spurious struct Class(pub id) interface is gone.

Tested with a new objc_class_type.h header covering the return, no-colon and parameter cases. cargo test -p bindgen-tests: 640 passed, 1 failed (objc_template, which fails identically on the base commit with the libclang on my machine). cargo fmt --check and the CI clippy invocation are clean.

r? @pvdrz

`CXType_ObjCClass` shared a match arm with `CXType_ObjCInterface`, so
the builtin `Class` type was parsed with `ObjCInterface::from_ty` on
whatever cursor the type was seen at. For a method return type that is
the method cursor, so the invented interface was named after the
selector and `rust_ident("take:")` panicked. Without a colon it did not
panic but still generated a bogus `struct cls(pub id)` interface.

Add `TypeKind::ObjCClass`, treated like `ObjCId` everywhere and
generated as `*mut objc::runtime::Class`.

Fixes rust-lang#3484.
@emilio
emilio added this pull request to the merge queue Oct 27, 2026
Merged via the queue into rust-lang:main with commit 7249e69 Oct 27, 2026
52 checks passed
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.

Method returning Class panics when the selector contains a colon ("take:" is not a valid Ident)

3 participants