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

[SR-13515] Incorrect fix-it when attempting to extend an objc protocol with a default implementation #55951

Open
amomchilov opened this issue Sep 7, 2020 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@amomchilov
Copy link
Contributor

Previous ID SR-13515
Radar rdar://problem/68476523
Original Reporter @amomchilov
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @theblixguy
Priority Medium

md5: 1e0dc6ae6b02f489a3c7f3c42370108b

Issue Description:

@objc protocols don't support default implementations for methods via extensions. Attempting to do so makes the compiler suggest that you add the @objc attribute to your default method implementation, which instead it should tell you that the entire construct is invalid.

Example code:

import Foundation

@objc protocol ObjCProtocol {
    @objc func requiredMethod()
}

extension ObjCProtocol {
    /* @objc */ func requiredMethod() {
        print("Default implementation")
    }
}

class ConformingClass: NSObject, ObjCProtocol {
    
}

ConformingClass().requiredMethod()

Error:

Untitled.swift:13:7: error: non-'@objc' method 'requiredMethod()' does not satisfy requirement of '@objc' protocol 'ObjCProtocol'
class ConformingClass: NSObject, ObjCProtocol {
      ^
Untitled.swift:8:19: note: add '@objc' to expose this instance method to Objective-C
        /* @objc */ func requiredMethod() {
                         ^
                    @objc 
Untitled.swift:4:13: note: requirement 'requiredMethod()' declared here
        @objc func requiredMethod()
                   ^

If you follow the fix-it, you another error which correctly tells you that this is invalid. But strangely, you still get a suggestion to add @objc:

Untitled.swift:8:3: error: @objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
        @objc func requiredMethod() {
        ~^~~~~
        
Untitled.swift:13:7: error: non-'@objc' method 'requiredMethod()' does not satisfy requirement of '@objc' protocol 'ObjCProtocol'
class ConformingClass: NSObject, ObjCProtocol {
      ^
Untitled.swift:8:13: note: add '@objc' to expose this instance method to Objective-C
        @objc func requiredMethod() {
                   ^
        @objc 
Untitled.swift:4:13: note: requirement 'requiredMethod()' declared here
        @objc func requiredMethod()
                   ^
@typesanitizer
Copy link

@swift-ci create

@theblixguy
Copy link
Collaborator

#33891

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants