Skip to content

Commit d60a067

Browse files
authored
Merge pull request #71050 from hborla/global-actor-unsafe
[Concurrency] Deprecate `@GlobalActor(unsafe)` in favor of `@preconcurrency @GlobalActor`
2 parents 5b05e17 + 2a18edc commit d60a067

32 files changed

+172
-153
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ class ActorIsolation {
6666
/// The declaration is isolated to a global actor. It can refer to other
6767
/// entities with the same global actor.
6868
GlobalActor,
69-
/// The declaration is isolated to a global actor but with the "unsafe"
70-
/// annotation, which means that we only enforce the isolation if we're
71-
/// coming from something with specific isolation.
72-
GlobalActorUnsafe,
7369
};
7470

7571
private:
@@ -121,9 +117,8 @@ class ActorIsolation {
121117
return ActorIsolation(ActorInstance, capturedActor);
122118
}
123119

124-
static ActorIsolation forGlobalActor(Type globalActor, bool unsafe) {
125-
return ActorIsolation(
126-
unsafe ? GlobalActorUnsafe : GlobalActor, globalActor);
120+
static ActorIsolation forGlobalActor(Type globalActor) {
121+
return ActorIsolation(GlobalActor, globalActor);
127122
}
128123

129124
static std::optional<ActorIsolation> forSILString(StringRef string) {
@@ -140,7 +135,7 @@ class ActorIsolation {
140135
.Case("global_actor",
141136
std::optional<ActorIsolation>(ActorIsolation::GlobalActor))
142137
.Case("global_actor_unsafe", std::optional<ActorIsolation>(
143-
ActorIsolation::GlobalActorUnsafe))
138+
ActorIsolation::GlobalActor))
144139
.Default(std::nullopt);
145140
if (kind == std::nullopt)
146141
return std::nullopt;
@@ -171,7 +166,6 @@ class ActorIsolation {
171166
switch (getKind()) {
172167
case ActorInstance:
173168
case GlobalActor:
174-
case GlobalActorUnsafe:
175169
return true;
176170

177171
case Unspecified:
@@ -186,7 +180,7 @@ class ActorIsolation {
186180
VarDecl *getActorInstance() const;
187181

188182
bool isGlobalActor() const {
189-
return getKind() == GlobalActor || getKind() == GlobalActorUnsafe;
183+
return getKind() == GlobalActor;
190184
}
191185

192186
bool isMainActor() const;
@@ -238,7 +232,6 @@ class ActorIsolation {
238232
lhs.parameterIndex == rhs.parameterIndex);
239233

240234
case GlobalActor:
241-
case GlobalActorUnsafe:
242235
llvm_unreachable("Global actors handled above");
243236
}
244237
}
@@ -271,9 +264,6 @@ class ActorIsolation {
271264
case GlobalActor:
272265
os << "global_actor";
273266
return;
274-
case GlobalActorUnsafe:
275-
os << "global_actor_unsafe";
276-
return;
277267
}
278268
llvm_unreachable("Covered switch isn't covered?!");
279269
}

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5743,8 +5743,13 @@ ERROR(global_actor_on_local_variable,none,
57435743
ERROR(global_actor_on_storage_of_value_type,none,
57445744
"stored property %0 within struct cannot have a global actor",
57455745
(DeclName))
5746-
ERROR(global_actor_non_unsafe_init,none,
5747-
"global actor attribute %0 argument can only be '(unsafe)'", (Type))
5746+
ERROR(unsafe_global_actor,none,
5747+
"'(unsafe)' global actors are deprecated; "
5748+
"use '@preconcurrency' instead",
5749+
())
5750+
ERROR(global_actor_arg,none,
5751+
"global actor attribute %0 cannot have arguments",
5752+
(Type))
57485753
ERROR(global_actor_non_final_class,none,
57495754
"non-final class %0 cannot be a global actor", (DeclName))
57505755
ERROR(global_actor_top_level_var,none,

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,6 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
27902790
break;
27912791

27922792
case ActorIsolation::GlobalActor:
2793-
case ActorIsolation::GlobalActorUnsafe:
27942793
printFieldQuoted(isolation.getGlobalActor().getString(),
27952794
"global_actor_isolated", CapturesColor);
27962795
break;

lib/AST/Attr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,8 +1329,6 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13291329
type.print(Printer, Options);
13301330
else
13311331
attr->getTypeRepr()->print(Printer, Options);
1332-
if (attr->isArgUnsafe() && Options.IsForSwiftInterface)
1333-
Printer << "(unsafe)";
13341332
Printer.printNamePost(PrintNameContext::Attribute);
13351333
break;
13361334
}

lib/AST/Decl.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,15 +2512,20 @@ static bool deferMatchesEnclosingAccess(const FuncDecl *defer) {
25122512
if (type->isAnyActor())
25132513
return true;
25142514

2515-
switch (getActorIsolation(type)) {
2515+
auto isolation = getActorIsolation(type);
2516+
switch (isolation) {
25162517
case ActorIsolation::Unspecified:
25172518
case ActorIsolation::NonisolatedUnsafe:
2518-
case ActorIsolation::GlobalActorUnsafe:
25192519
break;
25202520

2521+
case ActorIsolation::GlobalActor:
2522+
if (isolation.preconcurrency())
2523+
break;
2524+
2525+
return true;
2526+
25212527
case ActorIsolation::ActorInstance:
25222528
case ActorIsolation::Nonisolated:
2523-
case ActorIsolation::GlobalActor:
25242529
return true;
25252530
}
25262531
}
@@ -10897,7 +10902,6 @@ bool VarDecl::isSelfParamCaptureIsolated() const {
1089710902
case ActorIsolation::Nonisolated:
1089810903
case ActorIsolation::NonisolatedUnsafe:
1089910904
case ActorIsolation::GlobalActor:
10900-
case ActorIsolation::GlobalActorUnsafe:
1090110905
return false;
1090210906

1090310907
case ActorIsolation::ActorInstance:
@@ -10965,9 +10969,9 @@ ActorIsolation swift::getActorIsolationOfContext(
1096510969
dcToUse->getASTContext().LangOpts.StrictConcurrencyLevel >=
1096610970
StrictConcurrency::Complete) {
1096710971
if (Type mainActor = dcToUse->getASTContext().getMainActorType())
10968-
return ActorIsolation::forGlobalActor(
10969-
mainActor,
10970-
/*unsafe=*/!dcToUse->getASTContext().isSwiftVersionAtLeast(6));
10972+
return ActorIsolation::forGlobalActor(mainActor)
10973+
.withPreconcurrency(
10974+
!dcToUse->getASTContext().isSwiftVersionAtLeast(6));
1097110975
}
1097210976
}
1097310977

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,7 @@ static void formatDiagnosticArgument(StringRef Modifier,
910910
Out << "actor-isolated";
911911
break;
912912

913-
case ActorIsolation::GlobalActor:
914-
case ActorIsolation::GlobalActorUnsafe: {
913+
case ActorIsolation::GlobalActor: {
915914
if (isolation.isMainActor()) {
916915
Out << "main actor-isolated";
917916
} else {

lib/AST/TypeCheckRequests.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,6 @@ bool ActorIsolation::requiresSubstitution() const {
16631663
return false;
16641664

16651665
case GlobalActor:
1666-
case GlobalActorUnsafe:
16671666
return getGlobalActor()->hasTypeParameter();
16681667
}
16691668
llvm_unreachable("unhandled actor isolation kind!");
@@ -1678,16 +1677,17 @@ ActorIsolation ActorIsolation::subst(SubstitutionMap subs) const {
16781677
return *this;
16791678

16801679
case GlobalActor:
1681-
case GlobalActorUnsafe:
1682-
return forGlobalActor(
1683-
getGlobalActor().subst(subs), kind == GlobalActorUnsafe)
1684-
.withPreconcurrency(preconcurrency());
1680+
return forGlobalActor(getGlobalActor().subst(subs))
1681+
.withPreconcurrency(preconcurrency());
16851682
}
16861683
llvm_unreachable("unhandled actor isolation kind!");
16871684
}
16881685

16891686
void swift::simple_display(
16901687
llvm::raw_ostream &out, const ActorIsolation &state) {
1688+
if (state.preconcurrency())
1689+
out << "preconcurrency ";
1690+
16911691
switch (state) {
16921692
case ActorIsolation::ActorInstance:
16931693
out << "actor-isolated to instance of ";
@@ -1715,16 +1715,12 @@ void swift::simple_display(
17151715
break;
17161716

17171717
case ActorIsolation::GlobalActor:
1718-
case ActorIsolation::GlobalActorUnsafe:
17191718
out << "actor-isolated to global actor ";
17201719
if (state.isSILParsed()) {
17211720
out << "SILPARSED";
17221721
} else {
17231722
out << state.getGlobalActor().getString();
17241723
}
1725-
1726-
if (state == ActorIsolation::GlobalActorUnsafe)
1727-
out << "(unsafe)";
17281724
break;
17291725
}
17301726
}

lib/IDE/CompletionLookup.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,15 @@ void CompletionLookup::analyzeActorIsolation(
767767
}
768768
break;
769769
}
770-
case ActorIsolation::GlobalActorUnsafe:
771-
// For "unsafe" global actor isolation, automatic 'async' only happens
770+
case ActorIsolation::GlobalActor: {
771+
// For "preconcurrency" global actor isolation, automatic 'async' only happens
772772
// if the context has adopted concurrency.
773-
if (!CanCurrDeclContextHandleAsync &&
773+
if (isolation.preconcurrency() &&
774+
!CanCurrDeclContextHandleAsync &&
774775
!completionContextUsesConcurrencyFeatures(CurrDeclContext)) {
775776
return;
776777
}
777-
LLVM_FALLTHROUGH;
778-
case ActorIsolation::GlobalActor: {
778+
779779
auto getClosureActorIsolation = [this](AbstractClosureExpr *CE) {
780780
// Prefer solution-specific actor-isolations and fall back to the one
781781
// recorded in the AST.

lib/SILGen/SILGenApply.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,6 @@ static void emitDelayedArguments(SILGenFunction &SGF,
30513051
SILValue executor;
30523052
switch (*defaultArgIsolation) {
30533053
case ActorIsolation::GlobalActor:
3054-
case ActorIsolation::GlobalActorUnsafe:
30553054
executor = SGF.emitLoadGlobalActorExecutor(
30563055
defaultArgIsolation->getGlobalActor());
30573056
break;
@@ -5630,7 +5629,6 @@ RValue SILGenFunction::emitApply(
56305629
break;
56315630

56325631
case ActorIsolation::GlobalActor:
5633-
case ActorIsolation::GlobalActorUnsafe:
56345632
executor = emitLoadGlobalActorExecutor(
56355633
implicitActorHopTarget->getGlobalActor());
56365634
break;

lib/SILGen/SILGenConstructor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ static bool ctorHopsInjectedByDefiniteInit(ConstructorDecl *ctor,
611611
case ActorIsolation::Nonisolated:
612612
case ActorIsolation::NonisolatedUnsafe:
613613
case ActorIsolation::GlobalActor:
614-
case ActorIsolation::GlobalActorUnsafe:
615614
return false;
616615
}
617616
}
@@ -1582,7 +1581,6 @@ void SILGenFunction::emitMemberInitializer(DeclContext *dc, VarDecl *selfDecl,
15821581
break;
15831582

15841583
case ActorIsolation::GlobalActor:
1585-
case ActorIsolation::GlobalActorUnsafe:
15861584
case ActorIsolation::ActorInstance: {
15871585
if (requiredIsolation != contextIsolation) {
15881586
// Implicit initializers diagnose actor isolation violations

0 commit comments

Comments
 (0)