@@ -348,6 +348,10 @@ swift::checkGlobalActorAttributes(SourceLoc loc, DeclContext *dc,
348
348
return std::make_pair (globalActorAttr, globalActorNominal);
349
349
}
350
350
351
+ static bool shouldIgnoreGlobalActorOnAccessor (const ASTContext &ctx) {
352
+ return ctx.isSwiftVersionAtLeast (6 );
353
+ }
354
+
351
355
std::optional<std::pair<CustomAttr *, NominalTypeDecl *>>
352
356
GlobalActorAttributeRequest::evaluate (
353
357
Evaluator &evaluator,
@@ -452,12 +456,18 @@ GlobalActorAttributeRequest::evaluate(
452
456
auto &ctx = accessor->getASTContext ();
453
457
const auto attrRange = globalActorAttr->getRangeWithAt ();
454
458
const unsigned langModeForError = accessor->isGetter () ? 7 : 6 ;
459
+ const bool isError = ctx.isSwiftVersionAtLeast (langModeForError);
455
460
456
461
// Complain about a global actor attribute on an accessor.
457
462
accessor->diagnose (diag::global_actor_disallowed, decl)
458
463
.highlight (attrRange)
459
464
.warnUntilSwiftVersion (langModeForError);
460
465
466
+ if (!isError && shouldIgnoreGlobalActorOnAccessor (ctx)) {
467
+ ctx.Diags .diagnose (attrRange.Start , diag::global_actor_attr_is_ignored)
468
+ .highlight (attrRange);
469
+ }
470
+
461
471
auto *storage = accessor->getStorage ();
462
472
463
473
// Suggest to move the attribute to the storage if it is legal and the
@@ -476,7 +486,7 @@ GlobalActorAttributeRequest::evaluate(
476
486
}
477
487
478
488
// Fail if we emitted an error.
479
- if (ctx. isSwiftVersionAtLeast (langModeForError) )
489
+ if (isError )
480
490
return std::nullopt;
481
491
482
492
} else if (isa<ExtensionDecl>(decl) || isa<AbstractFunctionDecl>(decl)) {
@@ -5458,9 +5468,11 @@ getActorIsolationForMainFuncDecl(FuncDecl *fnDecl) {
5458
5468
// / Check rules related to global actor attributes on a class declaration.
5459
5469
// /
5460
5470
// / \returns true if an error occurred.
5461
- static bool checkClassGlobalActorIsolation (
5462
- ClassDecl *classDecl, ActorIsolation isolation) {
5463
- assert (isolation.isGlobalActor ());
5471
+ static bool checkClassIsolation (ClassDecl *classDecl,
5472
+ ActorIsolation isolation) {
5473
+ if (!isolation.isGlobalActor ()) {
5474
+ return false ;
5475
+ }
5464
5476
5465
5477
// A class can only be annotated with a global actor if it has no
5466
5478
// superclass, the superclass is annotated with the same global actor, or
@@ -5900,8 +5912,7 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
5900
5912
if (isolationFromAttr && isolationFromAttr->isGlobalActor ()) {
5901
5913
if (!areTypesEqual (isolationFromAttr->getGlobalActor (),
5902
5914
mainIsolation->getGlobalActor ())) {
5903
- fd->getASTContext ().Diags .diagnose (
5904
- fd->getLoc (), diag::main_function_must_be_mainActor);
5915
+ fd->diagnose (diag::main_function_must_be_mainActor);
5905
5916
}
5906
5917
}
5907
5918
return {
@@ -5911,17 +5922,29 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
5911
5922
}
5912
5923
}
5913
5924
5914
- // If this declaration has one of the actor isolation attributes, report
5915
- // that.
5925
+ // If this declaration has one of the actor isolation attributes, consider
5926
+ // reporting that.
5916
5927
if (isolationFromAttr) {
5917
- // Classes with global actors have additional rules regarding inheritance.
5918
- if (isolationFromAttr->isGlobalActor ()) {
5919
- if (auto classDecl = dyn_cast<ClassDecl>(value))
5920
- checkClassGlobalActorIsolation (classDecl, *isolationFromAttr);
5928
+ if (isa<AccessorDecl>(value) && shouldIgnoreGlobalActorOnAccessor (ctx)) {
5929
+ // Global actor attributes on accessors are deprecated, whereas other
5930
+ // isolation attributes are unconditionally unapplicable to accessors.
5931
+ //
5932
+ // In Swift 6 mode and onward, disregard the global actor attribute and
5933
+ // proceed to infer the isolation from the storage declaration.
5934
+ } else {
5935
+ // Classes with global actors have additional rules regarding inheritance.
5936
+ if (auto classDecl = dyn_cast<ClassDecl>(value)) {
5937
+ checkClassIsolation (classDecl, *isolationFromAttr);
5938
+ }
5939
+
5940
+ return {*isolationFromAttr,
5941
+ IsolationSource (/* source*/ nullptr , IsolationSource::Explicit)};
5921
5942
}
5943
+ }
5922
5944
5923
- return {*isolationFromAttr,
5924
- IsolationSource (/* source*/ nullptr , IsolationSource::Explicit)};
5945
+ // For an accessor, use the actor isolation of its storage declaration.
5946
+ if (auto *accessor = dyn_cast<AccessorDecl>(value)) {
5947
+ return getInferredActorIsolation (accessor->getStorage ());
5925
5948
}
5926
5949
5927
5950
InferredActorIsolation defaultIsolation;
@@ -6037,12 +6060,6 @@ static InferredActorIsolation computeActorIsolation(Evaluator &evaluator,
6037
6060
}
6038
6061
}
6039
6062
6040
- // If this is an accessor, use the actor isolation of its storage
6041
- // declaration.
6042
- if (auto accessor = dyn_cast<AccessorDecl>(value)) {
6043
- return getInferredActorIsolation (accessor->getStorage ());
6044
- }
6045
-
6046
6063
if (auto var = dyn_cast<VarDecl>(value)) {
6047
6064
auto &ctx = var->getASTContext ();
6048
6065
if (!ctx.LangOpts .isConcurrencyModelTaskToThread () &&
0 commit comments