Skip to content

Commit 37e15db

Browse files
committed
[CSDiagnostics] Use expr's start loc for 'self.' fix-it
Resolves SR-11787.
1 parent 8d208be commit 37e15db

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,10 @@ bool AssignmentFailure::diagnoseAsError() {
16471647
});
16481648

16491649
if (foundProperty != results.end()) {
1650-
emitDiagnostic(Loc, diag::masked_instance_variable,
1650+
auto startLoc = immutableExpr->getStartLoc();
1651+
emitDiagnostic(startLoc, diag::masked_instance_variable,
16511652
typeContext->getSelfTypeInContext())
1652-
.fixItInsert(Loc, "self.");
1653+
.fixItInsert(startLoc, "self.");
16531654
}
16541655
}
16551656

test/Sema/immutability.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,4 +712,17 @@ struct S {
712712
x += 1 // expected-error {{left side of mutating operator isn't mutable: 'x' is a 'let' constant}}
713713
}
714714
}
715+
716+
func bar() {
717+
// SR-11787: Make sure we insert "self." in the right location.
718+
let x = 0 // expected-note 3{{change 'let' to 'var' to make it mutable}}
719+
x += 1 // expected-error {{left side of mutating operator isn't mutable: 'x' is a 'let' constant}}
720+
// expected-note@-1 {{add explicit 'self.' to refer to mutable property of 'S'}} {{5-5=self.}}
721+
722+
(try x) += 1 // expected-error {{left side of mutating operator isn't mutable: 'x' is a 'let' constant}}
723+
// expected-note@-1 {{add explicit 'self.' to refer to mutable property of 'S'}} {{10-10=self.}}
724+
725+
x = 1 // expected-error {{cannot assign to value: 'x' is a 'let' constant}}
726+
// expected-note@-1 {{add explicit 'self.' to refer to mutable property of 'S'}} {{5-5=self.}}
727+
}
715728
}

0 commit comments

Comments
 (0)