Skip to content

Commit de86cc0

Browse files
committed
[CoroutineAccessors] Tweak diags for old accessors
When the feature is enabled, refer to the old accessors as they are actually spelled (i.e. `_read` and `_modify`).
1 parent 7c55892 commit de86cc0

File tree

7 files changed

+82
-51
lines changed

7 files changed

+82
-51
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9696,8 +9696,11 @@ const ParamDecl *getParameterAt(const ValueDecl *source, unsigned index);
96969696
/// nullptr if the source does not have a parameter list.
96979697
const ParamDecl *getParameterAt(const DeclContext *source, unsigned index);
96989698

9699-
StringRef getAccessorNameForDiagnostic(AccessorDecl *accessor, bool article);
9700-
StringRef getAccessorNameForDiagnostic(AccessorKind accessorKind, bool article);
9699+
StringRef
9700+
getAccessorNameForDiagnostic(AccessorDecl *accessor, bool article,
9701+
std::optional<bool> underscored = std::nullopt);
9702+
StringRef getAccessorNameForDiagnostic(AccessorKind accessorKind, bool article,
9703+
bool underscored);
97019704

97029705
void simple_display(llvm::raw_ostream &out,
97039706
OptionSet<NominalTypeDecl::LookupDirectFlags> options);

lib/AST/Decl.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6863,7 +6863,7 @@ bool ProtocolDecl::hasCircularInheritedProtocols() const {
68636863

68646864
/// Returns a descriptive name for the given accessor/addressor kind.
68656865
StringRef swift::getAccessorNameForDiagnostic(AccessorKind accessorKind,
6866-
bool article) {
6866+
bool article, bool underscored) {
68676867
switch (accessorKind) {
68686868
case AccessorKind::Get:
68696869
return article ? "a getter" : "getter";
@@ -6876,9 +6876,17 @@ StringRef swift::getAccessorNameForDiagnostic(AccessorKind accessorKind,
68766876
case AccessorKind::MutableAddress:
68776877
return article ? "a mutable addressor" : "mutable addressor";
68786878
case AccessorKind::Read:
6879+
if (underscored)
6880+
return article ? "a '_read' accessor" : "'_read' accessor";
6881+
// Fall through to the non-underscored spelling.
6882+
LLVM_FALLTHROUGH;
68796883
case AccessorKind::Read2:
68806884
return article ? "a 'read' accessor" : "'read' accessor";
68816885
case AccessorKind::Modify:
6886+
if (underscored)
6887+
return article ? "a '_modify' accessor" : "'_modify' accessor";
6888+
// Fall through to the non-underscored spelling.
6889+
LLVM_FALLTHROUGH;
68826890
case AccessorKind::Modify2:
68836891
return article ? "a 'modify' accessor" : "'modify' accessor";
68846892
case AccessorKind::WillSet:
@@ -6892,9 +6900,12 @@ StringRef swift::getAccessorNameForDiagnostic(AccessorKind accessorKind,
68926900
}
68936901

68946902
StringRef swift::getAccessorNameForDiagnostic(AccessorDecl *accessor,
6895-
bool article) {
6896-
return getAccessorNameForDiagnostic(accessor->getAccessorKind(),
6897-
article);
6903+
bool article,
6904+
std::optional<bool> underscored) {
6905+
return getAccessorNameForDiagnostic(
6906+
accessor->getAccessorKind(), article,
6907+
underscored.value_or(accessor->getASTContext().LangOpts.hasFeature(
6908+
Feature::CoroutineAccessors)));
68986909
}
68996910

69006911
bool AbstractStorageDecl::hasStorage() const {

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7914,15 +7914,17 @@ bool Parser::parseAccessorAfterIntroducer(
79147914
if (requiresFeatureCoroutineAccessors(Kind) &&
79157915
!Context.LangOpts.hasFeature(Feature::CoroutineAccessors)) {
79167916
diagnose(Tok, diag::accessor_requires_coroutine_accessors,
7917-
getAccessorNameForDiagnostic(Kind, /*article*/ false));
7917+
getAccessorNameForDiagnostic(Kind, /*article*/ false,
7918+
/*underscored*/ false));
79187919
}
79197920

79207921
// There should be no body in the limited syntax; diagnose unexpected
79217922
// accessor implementations.
79227923
if (parsingLimitedSyntax) {
79237924
if (Tok.is(tok::l_brace))
79247925
diagnose(Tok, diag::unexpected_getset_implementation_in_protocol,
7925-
getAccessorNameForDiagnostic(Kind, /*article*/ false));
7926+
getAccessorNameForDiagnostic(Kind, /*article*/ false,
7927+
/*underscored*/ false));
79267928
return false;
79277929
}
79287930

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3644,7 +3644,7 @@ static FuncDecl *findSimilarAccessor(DeclNameRef replacedVarName,
36443644
origStorage->getWriteImpl() == WriteImplKind::Stored)) {
36453645
Diags.diagnose(attr->getLocation(),
36463646
diag::dynamic_replacement_accessor_not_explicit,
3647-
getAccessorNameForDiagnostic(origAccessor->getAccessorKind(),
3647+
getAccessorNameForDiagnostic(origAccessor,
36483648
/*article=*/false),
36493649
origStorage->getName());
36503650
attr->setInvalid();

lib/Sema/TypeCheckStorage.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,19 @@ static void diagnoseReadWriteMutatingnessMismatch(
717717
auto disagreesWithWriter = (isWriterMutating != isModifierMutating);
718718
auto disagreesWithBoth = disagreesWithReader && disagreesWithWriter;
719719

720+
bool hasCoroutineAccessorFeature =
721+
storage->getASTContext().LangOpts.hasFeature(Feature::CoroutineAccessors);
722+
720723
auto readerAccessor = directAccessorKindForReadImpl(storage->getReadImpl());
721724
StringRef readerAccessorName =
722725
readerAccessor.has_value()
723-
? getAccessorNameForDiagnostic(*readerAccessor, /*article=*/false)
726+
? getAccessorNameForDiagnostic(
727+
*readerAccessor, /*article=*/false,
728+
/*underscored=*/hasCoroutineAccessorFeature)
724729
: "the inherited accessor";
725730
StringRef writerAccessorName =
726-
getAccessorNameForDiagnostic(writerAccesor, /*article=*/false);
731+
getAccessorNameForDiagnostic(writerAccesor, /*article=*/false,
732+
/*underscored=*/hasCoroutineAccessorFeature);
727733
unsigned diagnosticForm;
728734
if (isModifierMutating) {
729735
// modifier can't be mutating when both the setter is nonmutating and the
@@ -745,16 +751,20 @@ static void diagnoseReadWriteMutatingnessMismatch(
745751

746752
modifyAccessor->diagnose(
747753
diag::readwriter_mutatingness_differs_from_reader_or_writer_mutatingness,
748-
getAccessorNameForDiagnostic(readWriterAccessor, /*article=*/false),
754+
getAccessorNameForDiagnostic(readWriterAccessor, /*article=*/false,
755+
/*underscored=*/hasCoroutineAccessorFeature),
749756
isModifierMutating ? SelfAccessKind::Mutating
750757
: SelfAccessKind::NonMutating,
751758
diagnosticForm, writerAccessorName, SelfAccessKind::NonMutating,
752759
readerAccessorName, SelfAccessKind::Mutating);
753760
auto *writer = storage->getParsedAccessor(writerAccesor);
754761
if (disagreesWithWriter && writer) {
755-
writer->diagnose(
756-
diag::previous_accessor,
757-
getAccessorNameForDiagnostic(writerAccesor, /*article=*/false), 0);
762+
writer->diagnose(diag::previous_accessor,
763+
getAccessorNameForDiagnostic(
764+
writerAccesor,
765+
/*article=*/false,
766+
/*underscored=*/hasCoroutineAccessorFeature),
767+
0);
758768
}
759769
AccessorDecl *reader = nullptr;
760770
if (disagreesWithReader && readerAccessor.has_value() &&
@@ -3960,6 +3970,8 @@ StorageImplInfoRequest::evaluate(Evaluator &evaluator,
39603970

39613971
bool hasWillSet = storage->getParsedAccessor(AccessorKind::WillSet);
39623972
bool hasDidSet = storage->getParsedAccessor(AccessorKind::DidSet);
3973+
bool hasCoroutineAccessorFeature =
3974+
storage->getASTContext().LangOpts.hasFeature(Feature::CoroutineAccessors);
39633975
if ((hasWillSet || hasDidSet) && !isa<SubscriptDecl>(storage)) {
39643976
// Observers conflict with non-observers.
39653977
AccessorDecl *firstNonObserver = nullptr;
@@ -3972,19 +3984,21 @@ StorageImplInfoRequest::evaluate(Evaluator &evaluator,
39723984

39733985
if (firstNonObserver) {
39743986
if (auto willSet = storage->getParsedAccessor(AccessorKind::WillSet)) {
3975-
willSet->diagnose(
3976-
diag::observing_accessor_conflicts_with_accessor, 0,
3977-
getAccessorNameForDiagnostic(
3978-
firstNonObserver->getAccessorKind(), /*article=*/ true));
3987+
willSet->diagnose(diag::observing_accessor_conflicts_with_accessor, 0,
3988+
getAccessorNameForDiagnostic(
3989+
firstNonObserver->getAccessorKind(),
3990+
/*article=*/true,
3991+
/*underscored=*/hasCoroutineAccessorFeature));
39793992
willSet->setInvalid();
39803993
hasWillSet = false;
39813994
}
39823995

39833996
if (auto didSet = storage->getParsedAccessor(AccessorKind::DidSet)) {
3984-
didSet->diagnose(
3985-
diag::observing_accessor_conflicts_with_accessor, 1,
3986-
getAccessorNameForDiagnostic(
3987-
firstNonObserver->getAccessorKind(), /*article=*/ true));
3997+
didSet->diagnose(diag::observing_accessor_conflicts_with_accessor, 1,
3998+
getAccessorNameForDiagnostic(
3999+
firstNonObserver->getAccessorKind(),
4000+
/*article=*/true,
4001+
/*underscored=*/hasCoroutineAccessorFeature));
39884002
didSet->setInvalid();
39894003
hasDidSet = false;
39904004
}

test/Parse/coroutine_accessors.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,17 @@ var im_m: Int {
261261
modify { // expected-disabled-error{{cannot_find_in_scope}}
262262
fatalError()
263263
}
264-
_modify { // expected-enabled-error{{variable with a 'modify' accessor must also have a getter, addressor, or 'read' accessor}}
265-
// expected-disabled-error@-1{{cannot_find_in_scope}}
264+
_modify { // expected-enabled-error{{variable with a '_modify' accessor must also have a getter, addressor, or 'read' accessor}}
265+
// expected-disabled-error@-1{{cannot_find_in_scope}}
266266
fatalError()
267267
}
268268
}
269269

270270
// enabled: need a reader.
271271
// disabled: implicit getter.
272272
var i_mm: Int {
273-
_modify { // expected-error{{variable with a 'modify' accessor must also have a getter, addressor, or 'read' accessor}}
273+
_modify { // expected-enabled-error{{variable with a '_modify' accessor must also have a getter, addressor, or 'read' accessor}}
274+
// expected-disabled-error@-1{{variable with a 'modify' accessor must also have a getter, addressor, or 'read' accessor}}
274275
fatalError()
275276
}
276277
modify { // expected-disabled-error{{'modify' accessor is only valid when experimental feature coroutine accessors is enabled}}

test/Sema/coroutine_accessors.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var ingnsn_m: Int {
3434
var ignsn_m: Int {
3535
mutating get { 0 }
3636
nonmutating set {}
37-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the getter is 'mutating'}}
37+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the getter is 'mutating'}}
3838
// expected-note@-3{{getter defined here}}
3939
var fake: Int
4040
yield &fake
@@ -43,7 +43,7 @@ var ignsn_m: Int {
4343
var ingsn_m: Int {
4444
get { 0 }
4545
set {}
46-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
46+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
4747
// expected-note@-2{{setter defined here}}
4848
var fake: Int
4949
yield &fake
@@ -52,7 +52,7 @@ var ingsn_m: Int {
5252
var igsn_m: Int {
5353
mutating get { 0 }
5454
set {}
55-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the getter is 'mutating'}}
55+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the getter is 'mutating'}}
5656
// expected-note@-2{{setter defined here}}
5757
// expected-note@-4{{getter defined here}}
5858
var fake: Int
@@ -62,7 +62,7 @@ var igsn_m: Int {
6262
var ingns_m: Int {
6363
get { 0 }
6464
nonmutating set {}
65-
_modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the getter is not 'mutating'}}
65+
_modify { // expected-error{{'_modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the getter is not 'mutating'}}
6666
// expected-note@-2{{setter defined here}}
6767
// expected-note@-4{{getter defined here}}
6868
yield &i
@@ -116,16 +116,16 @@ var in_rnsn_m: Int {
116116
var i_rnsn_m: Int {
117117
mutating _read { yield i }
118118
nonmutating set {}
119-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the 'read' accessor is 'mutating'}}
120-
// expected-note@-3{{'read' accessor defined here}}
119+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the '_read' accessor is 'mutating'}}
120+
// expected-note@-3{{'_read' accessor defined here}}
121121
var fake: Int
122122
yield &fake
123123
}
124124
}
125125
var in_rsn_m: Int {
126126
_read { yield i }
127127
set {}
128-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
128+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
129129
// expected-note@-2{{setter defined here}}
130130
var fake: Int
131131
yield &fake
@@ -134,19 +134,19 @@ var in_rsn_m: Int {
134134
var i_rsn_m: Int {
135135
mutating _read { yield i }
136136
set {}
137-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the 'read' accessor is 'mutating'}}
137+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the '_read' accessor is 'mutating'}}
138138
// expected-note@-2{{setter defined here}}
139-
// expected-note@-4{{'read' accessor defined here}}
139+
// expected-note@-4{{'_read' accessor defined here}}
140140
var fake: Int
141141
yield &fake
142142
}
143143
}
144144
var in_rns_m: Int {
145145
_read { yield i }
146146
nonmutating set {}
147-
_modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the 'read' accessor is not 'mutating'}}
147+
_modify { // expected-error{{'_modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the '_read' accessor is not 'mutating'}}
148148
// expected-note@-2{{setter defined here}}
149-
// expected-note@-4{{'read' accessor defined here}}
149+
// expected-note@-4{{'_read' accessor defined here}}
150150
yield &i
151151
}
152152
}
@@ -198,7 +198,7 @@ var inrnsn_m: Int {
198198
var irnsn_m: Int {
199199
mutating read { yield i }
200200
nonmutating set {}
201-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the 'read' accessor is 'mutating'}}
201+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the 'read' accessor is 'mutating'}}
202202
// expected-note@-3{{'read' accessor defined here}}
203203
var fake: Int
204204
yield &fake
@@ -207,7 +207,7 @@ var irnsn_m: Int {
207207
var inrsn_m: Int {
208208
read { yield i }
209209
set {}
210-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
210+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
211211
// expected-note@-2{{setter defined here}}
212212
var fake: Int
213213
yield &fake
@@ -216,7 +216,7 @@ var inrsn_m: Int {
216216
var irsn_m: Int {
217217
mutating read { yield i }
218218
set {}
219-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the 'read' accessor is 'mutating'}}
219+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the 'read' accessor is 'mutating'}}
220220
// expected-note@-2{{setter defined here}}
221221
// expected-note@-4{{'read' accessor defined here}}
222222
var fake: Int
@@ -226,7 +226,7 @@ var irsn_m: Int {
226226
var inrns_m: Int {
227227
read { yield i }
228228
nonmutating set {}
229-
_modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the 'read' accessor is not 'mutating'}}
229+
_modify { // expected-error{{'_modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the 'read' accessor is not 'mutating'}}
230230
// expected-note@-2{{setter defined here}}
231231
// expected-note@-4{{'read' accessor defined here}}
232232
yield &i
@@ -280,7 +280,7 @@ var inuansn_m: Int {
280280
var iuansn_m: Int {
281281
mutating unsafeAddress { UnsafePointer(bitPattern: 0x0)! }
282282
nonmutating set {}
283-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the addressor is 'mutating'}}
283+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the addressor is 'mutating'}}
284284
// expected-note@-3{{addressor defined here}}
285285
var fake: Int
286286
yield &fake
@@ -289,7 +289,7 @@ var iuansn_m: Int {
289289
var inuasn_m: Int {
290290
unsafeAddress { UnsafePointer(bitPattern: 0x0)! }
291291
set {}
292-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
292+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when the setter is not 'nonmutating'}}
293293
// expected-note@-2{{setter defined here}}
294294
var fake: Int
295295
yield &fake
@@ -298,7 +298,7 @@ var inuasn_m: Int {
298298
var iuasn_m: Int {
299299
mutating unsafeAddress { UnsafePointer(bitPattern: 0x0)! }
300300
set {}
301-
nonmutating _modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the addressor is 'mutating'}}
301+
nonmutating _modify { // expected-error{{'_modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the addressor is 'mutating'}}
302302
// expected-note@-2{{setter defined here}}
303303
// expected-note@-4{{addressor defined here}}
304304
var fake: Int
@@ -308,7 +308,7 @@ var iuasn_m: Int {
308308
var inuans_m: Int {
309309
unsafeAddress { UnsafePointer(bitPattern: 0x0)! }
310310
nonmutating set {}
311-
_modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the addressor is not 'mutating'}}
311+
_modify { // expected-error{{'_modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the addressor is not 'mutating'}}
312312
// expected-note@-2{{setter defined here}}
313313
// expected-note@-4{{addressor defined here}}
314314
yield &i
@@ -444,8 +444,8 @@ var in_rnsnm: Int {
444444
var i_rnsnm: Int {
445445
mutating _read { yield i }
446446
nonmutating set {}
447-
nonmutating modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the 'read' accessor is 'mutating'}}
448-
// expected-note@-3{{'read' accessor defined here}}
447+
nonmutating modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when the '_read' accessor is 'mutating'}}
448+
// expected-note@-3{{'_read' accessor defined here}}
449449
var fake: Int
450450
yield &fake
451451
}
@@ -462,19 +462,19 @@ var in_rsnm: Int {
462462
var i_rsnm: Int {
463463
mutating _read { yield i }
464464
set {}
465-
nonmutating modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the 'read' accessor is 'mutating'}}
465+
nonmutating modify { // expected-error{{'modify' accessor cannot be 'nonmutating' when either the setter is not 'nonmutating' or the '_read' accessor is 'mutating'}}
466466
// expected-note@-2{{setter defined here}}
467-
// expected-note@-4{{'read' accessor defined here}}
467+
// expected-note@-4{{'_read' accessor defined here}}
468468
var fake: Int
469469
yield &fake
470470
}
471471
}
472472
var in_rnsm: Int {
473473
_read { yield i }
474474
nonmutating set {}
475-
modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the 'read' accessor is not 'mutating'}}
475+
modify { // expected-error{{'modify' accessor cannot be 'mutating' when both the setter is 'nonmutating' and the '_read' accessor is not 'mutating'}}
476476
// expected-note@-2{{setter defined here}}
477-
// expected-note@-4{{'read' accessor defined here}}
477+
// expected-note@-4{{'_read' accessor defined here}}
478478
yield &i
479479
}
480480
}

0 commit comments

Comments
 (0)