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))
NOTE(static_functions_not_mutating_detail,none,
"static members cannot access instance properties or self", ())

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