Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obj-C ffigen appears to de-dup _objc_msgSend_* usage, but not generation #1594

Open
stuartmorgan opened this issue Sep 24, 2024 · 1 comment

Comments

@stuartmorgan
Copy link

I always have a scattering of unused _objc_msgSend_### warnings from my generated bindings. It also looks like there's some de-duping at the usage level but not the generation level for _objc_msgSend_*. For example, I have this in the NSScriptClassDescription generation section:

late final _sel_classDescriptionForClass_ =
    objc.registerName("classDescriptionForClass:");
final _objc_msgSend_686 = objc.msgSendPointer
    .cast<
        ffi.NativeFunction<
            ffi.Pointer<objc.ObjCObject> Function(
                ffi.Pointer<objc.ObjCObject>,
                ffi.Pointer<objc.ObjCSelector>,
                ffi.Pointer<objc.ObjCObject> aClass)>>()
    .asFunction<
        ffi.Pointer<objc.ObjCObject> Function(ffi.Pointer<objc.ObjCObject>,
            ffi.Pointer<objc.ObjCSelector>, ffi.Pointer<objc.ObjCObject>)>();

_objc_msgSend_686 is unused. When I looked for _sel_classDescriptionForClass_ to see why, I found these in two different classes:

  /// classDescriptionForClass:
  static NSClassDescription? classDescriptionForClass_(
      objc.ObjCObjectBase aClass) {
    final _ret = _objc_msgSend_736(_class_NSClassDescription,
        _sel_classDescriptionForClass_, aClass.ref.pointer);
    return _ret.address == 0
        ? null
        : NSClassDescription.castFromPointer(_ret, retain: true, release: true);
  }

...


  /// classDescriptionForClass:
  static NSClassDescription? classDescriptionForClass_(
      objc.ObjCObjectBase aClass) {
    final _ret = _objc_msgSend_736(_class_NSScriptClassDescription,
        _sel_classDescriptionForClass_, aClass.ref.pointer);
    return _ret.address == 0
        ? null
        : NSClassDescription.castFromPointer(_ret, retain: true, release: true);
  }

They both use the identical:

final _objc_msgSend_736 = objc.msgSendPointer
    .cast<
        ffi.NativeFunction<
            ffi.Pointer<objc.ObjCObject> Function(
                ffi.Pointer<objc.ObjCObject>,
                ffi.Pointer<objc.ObjCSelector>,
                ffi.Pointer<objc.ObjCObject> aClass)>>()
    .asFunction<
        ffi.Pointer<objc.ObjCObject> Function(ffi.Pointer<objc.ObjCObject>,
            ffi.Pointer<objc.ObjCSelector>, ffi.Pointer<objc.ObjCObject>)>();

from the NSClassDescription generation section.

If over-generating and letting tree shaking handle it is intentional (to make the generator logic simpler), the file should be generated with a file-level ignore for that warning. But in this case since there's clearly already some kind of global de-duping happening somewhere anyway, maybe it would be easy to make that include not generating the duplicate in the first place?

@liamappelbe
Copy link
Contributor

They are deduped at the generation level, but it's probably a similar issue to what I mentioned here. A method is being deleted or having it's type altered, and the old msgSend isn't cleaned up.

Another possibility in this case is that the deduping logic could be made more aggressive. Maybe there are cases where the msgSend deduping logic thinks two argument types are different, but they're actually the same. For example, nullability doesn't affect the msgSend function signature.

As I mentioned on the other bug, as a work around for now, just add some ignore_for_file rules to the preamble: option in your ffigen config.

@liamappelbe liamappelbe added this to the ffigen 16.0.0 milestone Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants