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-13593] Error message talks about extensions, but I haven't written any extensions #56028

Closed
swift-ci opened this issue Sep 24, 2020 · 5 comments
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 good first issue Good for newcomers type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13593
Radar rdar://problem/72775653
Original Reporter ken (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 12.0 (12A7209)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee mininny (JIRA)
Priority Medium

md5: 56c24f364de73d1f9030ad4dcd4dcb23

Issue Description:

I've read on other webpages why this isn't allowed:

import Foundation
class C: NSObject {
    override func hash(into hasher: inout Hasher) { }
}

But what's strange here is not that compilation fails, but the error message it gives: "Overriding declarations in extensions is not supported". That's wrong, right? I mean, maybe it's technically a true statement, but I didn't write an extension here, so it's not the reason this particular class failed to compile.

I think most of the confusion around this limitation could be avoided if the error message more accurately described the situation.

@typesanitizer
Copy link

The diagnostic I see is:

tmp.swift:3:19: error: overriding non-open instance method outside of its defining module
    override func hash(into hasher: inout Hasher) { }
                  ^
tmp.swift:3:19: error: overriding declarations in extensions is not supported
    override func hash(into hasher: inout Hasher) { }
                  ^
ObjectiveC.NSObject:4:17: note: overridden declaration is here
    public func hash(into hasher: inout Hasher)
                ^

I think the first error kinda' explains what the issue is, but I agree that the extension part of the diagnostic doesn't make sense by looking at the code.

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Minhyuk Kim (JIRA)

I created a PR here: #35465

@CodaFi
Copy link
Member

CodaFi commented Jan 19, 2021

This PR will provide a nice diagnostic for this common corner-case, but I do believe we need a general purpose fixup to the wording of the original diagnostic to fully resolve this. I'd like to see something like

overriding declarations in extensions is not supported

become

method 'hash(into:)' is declared in an extension of 'NSObject' and cannot be overridden

Ideally if we could emit a note that points to the declaration itself (we have a generic note for this: drag::kind_declared_here) that would be cool too.

@swift-ci
Copy link
Collaborator Author

Comment by Ken Harris (JIRA)

Ooooh. I was all ready to say that that's a separate issue, and then I re-read everything.

I just now realized what the original error message is trying to say. As written, the "extension" message is somewhat ambiguous, and my mind was focused on getting my program to work, not on picking apart compiler error messages. In hindsight, my first interpretation was obviously nonsensical, and the other possible interpretation (which I didn't even consider at the time) is the correct one.

Robert's alternate phrasing is terrific, and I fully support that. If the error message were changed to that, I would consider this issue resolved.

Minhyuk's helpful note for the "hash" case, in addition to the normal error message, would be a great addition, too.

@swift-ci
Copy link
Collaborator Author

Comment by Minhyuk Kim (JIRA)

Thanks for the great opinion @CodaFi!

As you suggested, I modified existing warning to:

`method 'foo()' is declared in extension of 'A' and cannot be overriden`

and

`method 'foo()' declared in 'A' cannot be overriden from extension`

I kept the special handling for NSObject.hash method like this for additional clarify for users:

`NSObject.hash(into:)` is not overridable; subclasses can customize hashing by overriding the `hash` property

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 good first issue Good for newcomers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants