File tree 2 files changed +30
-5
lines changed
2 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -5643,11 +5643,16 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
5643
5643
if (AD->isGetter () && !AD->getAccessorKeywordLoc ().isValid ())
5644
5644
return ;
5645
5645
}
5646
-
5646
+
5647
5647
auto &d = getASTContext ().Diags ;
5648
- d.diagnose (FD->getFuncLoc (), diag::change_to_mutating,
5649
- isa<AccessorDecl>(FD))
5650
- .fixItInsert (FD->getFuncLoc (), " mutating " );
5648
+ auto diags = d.diagnose (FD->getFuncLoc (), diag::change_to_mutating,
5649
+ isa<AccessorDecl>(FD));
5650
+ if (auto nonmutatingAttr =
5651
+ FD->getAttrs ().getAttribute <NonMutatingAttr>()) {
5652
+ diags.fixItReplace (nonmutatingAttr->getLocation (), " mutating" );
5653
+ } else {
5654
+ diags.fixItInsert (FD->getFuncLoc (), " mutating " );
5655
+ }
5651
5656
return ;
5652
5657
}
5653
5658
}
Original file line number Diff line number Diff line change @@ -159,7 +159,7 @@ struct SomeStruct {
159
159
return 42
160
160
}
161
161
nonmutating
162
- set { // expected-note {{mark accessor 'mutating' to make 'self' mutable}} {{5-5 =mutating }}
162
+ set { // expected-note {{mark accessor 'mutating' to make 'self' mutable}} {{5-16 =mutating}}
163
163
iv = newValue // expected-error {{cannot assign to property: 'self' is immutable}}
164
164
}
165
165
}
@@ -681,3 +681,23 @@ struct SS {
681
681
j = j // expected-error {{cannot assign to value: 'j' is a 'let' constant}}
682
682
}
683
683
}
684
+
685
+ protocol JustAProtocol {
686
+ var name : String { get set }
687
+ }
688
+
689
+ extension JustAProtocol {
690
+ var foo : String {
691
+ get { return name }
692
+ nonmutating set { name = newValue } // expected-error {{cannot assign to property: 'self' is immutable}}
693
+ // expected-note@-1 {{mark accessor 'mutating' to make 'self' mutable}}{{5-16=mutating}}
694
+ }
695
+
696
+ nonmutating func bar( ) { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-14=mutating}}
697
+ name = " Hello " // expected-error {{cannot assign to property: 'self' is immutable}}
698
+ }
699
+
700
+ func baz( ) { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-3=mutating }}
701
+ name = " World " // expected-error {{cannot assign to property: 'self' is immutable}}
702
+ }
703
+ }
You can’t perform that action at this time.
0 commit comments