Skip to content

[ffigen] Parse SWIFT_UNAVAILABLE annotation #2665

@liamappelbe

Description

@liamappelbe

In ObjC all classes have a zero-argument constructor (aka the default constructor), but that's not true in Swift. To work around this, when generating the ObjC wrapper header for a Swift class, the swift compiler annotates the default constructor with SWIFT_UNAVAILABLE:

SWIFT_CLASS("_TtC17BasicSwiftWrapper10DogWrapper")
@interface DogWrapper : NSObject
@property (nonatomic, copy) NSString * _Nonnull name;
@property (nonatomic, copy) NSString * _Nonnull breed;
- (nonnull instancetype)initWithName:(NSString * _Nonnull)name breed:(NSString * _Nonnull)breed OBJC_DESIGNATED_INITIALIZER;
- (nonnull instancetype)init SWIFT_UNAVAILABLE;
+ (nonnull instancetype)new SWIFT_UNAVAILABLE_MSG("-init is unavailable");
@end

Currently ffigen doesn't see the SWIFT_UNAVAILABLE annotation, so it generates the default constructor anyway. When the user tries to call this ctor, they get a runtime exception in swift.

So we need to parse this annotation and omit any initializers/methods that have it.

Also, it's possible that parts of ffigen are assuming that all ObjC classes have a default constructor, so we'll have to be on the look out for that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueA good starting issue for contributors (issues with this label will appear in /contribute)package:ffigen

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions