Skip to content

Incorrect method resolution for @objc @implementation when method name is duplicated #79634

Open
@harlanhaskins

Description

@harlanhaskins

Description

This is somewhat of a contrived example, but it took me a little while to figure out why Swift wasn't accepting my @objc @implementation method.

If you have an ObjC header like this:

@interface MyClass
- (void)myMethod;
@end

@interface MyClass (Category)
- (void)myMethod;
@end

Where the method name is erroneously duplicated in the main interface and in a category, and then try to implement it in a .m file:

@implementation MyClass (Category)
- (void)myMethod {}
@end

Clang correctly warns you that this may be unexpected:

<source>:10:1: warning: category is implementing a method which will also be implemented by its primary class [-Wobjc-protocol-method-implementation]
   10 | - (void)myMethod {}
      | ^
<source>:2:1: note: method 'myMethod' declared here
    2 | - (void)myMethod;
      | ^

(godbolt link: https://godbolt.org/z/9x36W5P18)

But if I implement this method in a Swift @objc @implementation, I don't get a warning, and Swift doesn't match the method implementation to either of the declarations:

@objc @implementation(Category)
extension MyClass {
    func myMethod() {}
}
MyClass.swift:3:10: error: instance method 'myMethod()' does not match any instance method declared in the headers for 'MyClass'; did you use the instance method's Swift name?
    func myMethod() {}

If I remove the method declaration from the main interface, it resolves correctly, but with an erroneously duplicated declaration there's nothing pointing you to the duplication.

Reproduction

// MyClass.h

@interface MyClass : NSObject
- (void)myMethod;
@end

@interface MyClass (Category)
- (void)myMethod;
@end
// MyClass.swift
@objc @implementation(Category)
extension MyClass {
    func myMethod() {}
}

Expected behavior

Either it should resolve this to implement -[MyClass(Category) myMethod] (which can technically exist alongside -[MyClass myMethod]), or it should point out that it is resolving to the main class's declaration of myMethod.

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @objcFeature → attributes: The @objc attributebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.clang importerArea → compiler: The clang importerobjective-c interopFeature: Interoperability with Objective-Ctriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions