Skip to content

Commit 6d6feb6

Browse files
committed
[CSDiagnostics] Support static "Type." fix-it
Check whether we found a static property, and if so, suggest inserting "Type." instead of "self.". Resolves SR-11788.
1 parent 37e15db commit 6d6feb6

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,9 +3424,9 @@ ERROR(assignment_bang_has_immutable_subcomponent,none,
34243424
NOTE(change_to_mutating,none,
34253425
"mark %select{method|accessor}0 'mutating' to make 'self' mutable",
34263426
(bool))
3427-
NOTE(masked_instance_variable,none,
3428-
"add explicit 'self.' to refer to mutable property of %0",
3429-
(Type))
3427+
NOTE(masked_mutable_property,none,
3428+
"add explicit '%0' to refer to mutable %1 of %2",
3429+
(StringRef, DescriptiveDeclKind, Type))
34303430

34313431
ERROR(assignment_let_property_delegating_init,none,
34323432
"'let' property %0 may not be initialized directly; use "

lib/Sema/CSDiagnostics.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,8 @@ bool AssignmentFailure::diagnoseAsError() {
16161616
emitDiagnostic(Loc, DeclDiagnostic, message)
16171617
.highlight(immutableExpr->getSourceRange());
16181618

1619-
// If there is a masked instance variable of the same type, emit a
1620-
// note to fixit prepend a 'self.'.
1619+
// If there is a masked property of the same type, emit a
1620+
// note to fixit prepend a 'self.' or 'Type.'.
16211621
if (auto typeContext = DC->getInnermostTypeContext()) {
16221622
SmallVector<ValueDecl *, 2> results;
16231623
DC->lookupQualified(typeContext->getSelfNominalTypeDecl(),
@@ -1648,9 +1648,20 @@ bool AssignmentFailure::diagnoseAsError() {
16481648

16491649
if (foundProperty != results.end()) {
16501650
auto startLoc = immutableExpr->getStartLoc();
1651-
emitDiagnostic(startLoc, diag::masked_instance_variable,
1652-
typeContext->getSelfTypeInContext())
1653-
.fixItInsert(startLoc, "self.");
1651+
auto *property = *foundProperty;
1652+
auto selfTy = typeContext->getSelfTypeInContext();
1653+
1654+
// If we found an instance property, suggest inserting "self.",
1655+
// otherwise suggest "Type." for a static property.
1656+
std::string fixItText;
1657+
if (property->isInstanceMember()) {
1658+
fixItText = "self.";
1659+
} else {
1660+
fixItText = selfTy->getString() + ".";
1661+
}
1662+
emitDiagnostic(startLoc, diag::masked_mutable_property,
1663+
fixItText, property->getDescriptiveKind(), selfTy)
1664+
.fixItInsert(startLoc, fixItText);
16541665
}
16551666
}
16561667

test/Sema/immutability.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ extension JustAProtocol {
704704

705705
struct S {
706706
var x = 0
707+
static var y = 0
707708

708709
struct Nested {
709710
func foo() {
@@ -724,5 +725,19 @@ struct S {
724725

725726
x = 1 // expected-error {{cannot assign to value: 'x' is a 'let' constant}}
726727
// expected-note@-1 {{add explicit 'self.' to refer to mutable property of 'S'}} {{5-5=self.}}
728+
729+
// SR-11788: Insert "Type." for a static property.
730+
let y = 0 // expected-note {{change 'let' to 'var' to make it mutable}}
731+
y += 1 // expected-error {{left side of mutating operator isn't mutable: 'y' is a 'let' constant}}
732+
// expected-note@-1 {{add explicit 'S.' to refer to mutable static property of 'S'}} {{5-5=S.}}
733+
}
734+
}
735+
736+
struct S2<T> {
737+
static var y: Int { get { 0 } set {} }
738+
func foo() {
739+
let y = 0 // expected-note {{change 'let' to 'var' to make it mutable}}
740+
y += 1 // expected-error {{left side of mutating operator isn't mutable: 'y' is a 'let' constant}}
741+
// expected-note@-1 {{add explicit 'S2<T>.' to refer to mutable static property of 'S2<T>'}} {{5-5=S2<T>.}}
727742
}
728743
}

0 commit comments

Comments
 (0)