rust-lang / rust-lang/rust-bindgen

objective-c generated classes should not implement `objc::Message`.

Open
#2,686 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
@interface MyClass
@end
Bindgen Invocation
$ bindgen input.h -- -x objective-c
Actual Results
...
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
pub struct MyClass(pub id);
...
unsafe impl objc::Message for MyClass {}
...
Expected Results

objc::Message should be implemented on types for which pointers to the type will accept messages. The types generated by bindgen are aliases of the basic object pointer type (*mut objc::runtime::Object), so implementing objc::Message is wrong and will always result in invalid memory access (e.g., a *const MyClass does not accept messages).

If bindgen were changed to generate classes which are intended to be cast from the basic object pointer type this would work, however such a change would be a breaking change for consumers.

That change would look something like

use objc::{self, class, msg_send, sel, sel_impl};
#[allow(non_camel_case_types)]
pub type id = *mut objc::runtime::Object;
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
pub struct MyClass;
impl std::ops::Deref for MyClass {
    type Target = objc::runtime::Object;
    fn deref(&self) -> &Self::Target {
        &self
    }
}
unsafe impl objc::Message for MyClass {}
impl MyClass {
    pub fn alloc() -> *mut Self {
        unsafe { msg_send!(class!(MyClass), alloc) }
    }
}
impl IMyClass for *mut MyClass {}
pub trait IMyClass: Sized + std::ops::Deref {}

But that's not really in scope for this bug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown input.h header and bindgen input.h -- -x objective-c invocation. Trace Objective-C class generation and the objc::Message emission; done means generated classes no longer implement objc::Message while preserving the existing pointer representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
objective-c, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.