Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,10 @@ 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", ())
"%0 modifier cannot be used on static functions",
(SelfAccessKind))
Comment on lines +1738 to +1739
Copy link
Collaborator

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:

Suggested change
"%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", ())
Comment on lines +1740 to +1741
Copy link
Collaborator

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


ERROR(readwriter_mutatingness_differs_from_reader_or_writer_mutatingness,none,
"%0 cannot be %1 when "
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,10 @@ void AttributeChecker::visitMutationAttr(DeclAttribute *attr) {
}
}
// Verify that we don't have a static function.
if (FD->isStatic())
diagnoseAndRemoveAttr(attr, diag::static_functions_not_mutating);
if (FD->isStatic()) {
diagnoseAndRemoveAttr(attr, diag::static_functions_not_mutating, attrModifier);
diagnose(FD, diag::static_functions_not_mutating_detail);
}
}

void AttributeChecker::visitDynamicAttr(DynamicAttr *attr) {
Expand Down
3 changes: 2 additions & 1 deletion test/Sema/immutability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ struct SomeStruct {

static let type_let = 5 // expected-note {{change 'let' to 'var' to make it mutable}} {{10-13=var}}

mutating static func f() { // expected-error {{static functions must not be declared mutating}} {{3-12=}}
mutating static func f() { // expected-error {{'mutating' modifier cannot be used on static functions}} {{3-12=}}
// expected-note@-1 {{static members cannot access instance properties or self}}
}

mutating func g() {
Expand Down
3 changes: 2 additions & 1 deletion test/decl/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ protocol ProtocolWithMutating {
mutating func test2(_ a: Int?) // expected-note {{previously declared}}
func test2(_ a: Int!) // expected-error{{invalid redeclaration of 'test2'}}

mutating static func classTest1() // expected-error {{static functions must not be declared mutating}} {{3-12=}} expected-note {{previously declared}}
mutating static func classTest1() // expected-error {{'mutating' modifier cannot be used on static functions}} {{3-12=}} expected-note {{previously declared}}
// expected-note@-1 {{static members cannot access instance properties or self}}
static func classTest1() // expected-error{{invalid redeclaration of 'classTest1()'}}
}

Expand Down
6 changes: 4 additions & 2 deletions test/decl/subscript/static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func useMyStruct() {

struct BadStruct {
static subscript(_ i: Int) -> String {
nonmutating get { fatalError() } // expected-error{{static functions must not be declared mutating}}
mutating set { fatalError() } // expected-error{{static functions must not be declared mutating}}
nonmutating get { fatalError() } // expected-error {{'nonmutating' modifier cannot be used on static functions}}
// expected-note@-1 {{static members cannot access instance properties or self}}
mutating set { fatalError() } // expected-error {{'mutating' modifier cannot be used on static functions}}
// expected-note@-1 {{static members cannot access instance properties or self}}
}
}

Expand Down