-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix the static_functions_not_mutating error messsage #78052
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution! I left a comment inline about the text and as a separate note: please modify existing tests (because the text is now different) or make sure that we have tests for both nonmutating
and mutating
attributes that exercise the new diagnostics before we can run CI and land this.
@@ -1718,7 +1718,7 @@ ERROR(functions_mutating_and_not,none, | |||
"method must not be declared both %0 and %1", | |||
(SelfAccessKind, SelfAccessKind)) | |||
ERROR(static_functions_not_mutating,none, | |||
"static functions must not be declared mutating", ()) | |||
"nonmutating or mutating modifiers cannot be used on static functions because they do not operate on instance properties or self", ()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of clamping mutating and nonmutating together it would be better to print one or the other depending on what the attribute was, a boolean flag could be used to distinguish between the two (i.e. no_overloads_match_exactly_in_call
does a similar thing reference vs. call).
Instead of "operate" we can use "cannot access" and the "because" part can be turned into a note.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xedin
Thanks for the advice!
I fixed based on your suggestion.
d0d9099
to
cb96921
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left one more small note inline. We also have a few tests that use the diagnostic and they'd need to be adjusted before we can land this:
test/Sema/immutability.swift:134
test/decl/subscript/static.swift:23
test/decl/subscript/static.swift:24
test/decl/overload.swift:413
@felixinkinto Next time, please ask us to assign you to an issue before you start working on one. A good first issue is a queue, not a race. We want to avoid instances of multiple people independently working on them. |
cb96921
to
e8b9b8c
Compare
@xedin sorry for the delay for long. I addressed the feedback. May I ask some questions to do behaviour checks?
|
@AnthonyLatsis |
@swift-ci please test Windows platform |
"%0 modifier cannot be used on static functions", | ||
(SelfAccessKind)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you incorporate the declaration like so, the message will be also be tailored for an accessor:
"%0 modifier cannot be used on static functions", | |
(SelfAccessKind)) | |
"%0 modifier cannot be used on %kindonly1", | |
(SelfAccessKind, const FuncDecl *)) |
We loose the plural, but I think that’s fine because the object remains indefinite.
NOTE(static_functions_not_mutating_detail,none, | ||
"static members cannot access instance properties or self", ()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not a fan of this interpretation because it’s misleading. We have this very problem with another diagnostic: #62909. You can access both self
or instance members inside a static method depending on what you mean by it.
Maybe we could borrow a more clear phrasing from the book. For example:
static members are defined on the type itself rather than on instances of the type
static members belong to the type itself rather than to instances of the type
static members are members of the type itself rather than instances of the type
Emission order does matter. Notes are always attached to a primary diagnostic such as an error or warning. The grouped diagnostic formatter probably chose to display the error below the note there because it wants a clear path from each message to its location. |
Fix #77835