Skip to content

Protocol requirement can be satisfied with an misnamed API inside another extension, causing runtime crash #85228

@an0

Description

@an0

Description

For protocols defined in ObjC, its API names need some transformation to conform to Swift naming conventions.

When using a class extension to conform to such a protocol, if API name incorrectly uses ObjC convention, compiler will catch it saying "XXX has been renamed to YYY".

However, if I declare the conformance using one extension but putt its API implementation in another extension, the incorrect API name won't be caught and a runtime crash will happen when the delegate API is called.

Reproduction

@protocol ListAdapterDelegate <NSObject>

- (void)listAdapter:(id)listAdapter
  willDisplayObject:(id)object
        atIndexPath:(NSIndexPath *)indexPath;

@end

@protocol ListDisplayDelegate <NSObject>

- (void)listAdapter:(id)listAdapter willDisplaySectionController:(id)sectionController;

@end
class Foo {}

extension Foo: ListDisplayDelegate {

    func listAdapter(_ listAdapter: Any, willDisplaySectionController sectionController: Any) {
        
    }

    // Wrong name but compiles
    func listAdapter(_ listAdapter: Any, willDisplayObject object: Any, atIndexPath indexPath: IndexPath) {
        
    }
    
}

extension Foo: ListAdapterDelegate {

// Correct one should be
//    func listAdapter(_ listAdapter: Any, willDisplay object: Any, at indexPath: IndexPath) {
//        
//    }
    
}

Stack dump

objc_exception_throw (Unknown Source:0)
-[NSObject(NSObject) doesNotRecognizeSelector:] (Unknown Source:0)

Expected behavior

Compiler catches the issue

Environment

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)

Additional information

No response

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.conformancesFeature → protocol: protocol conformancesobjective-c interopFeature: Interoperability with Objective-Crun-time crashBug → crash: Swift code crashed during execution

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions