Skip to content

Commit d7054a5

Browse files
committed
[concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases like constructors/destructors where we are pretty sure we want to not support this. This is part of my bringup strategy for changing nonisolated/unspecified to be caller isolation inheriting.
1 parent 72859f1 commit d7054a5

18 files changed

+119
-15
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class ActorIsolation {
6969
/// The actor isolation iss statically erased, as for a call to
7070
/// an isolated(any) function. This is not possible for declarations.
7171
Erased,
72+
/// Inherits isolation from the caller of the given function.
73+
///
74+
/// DISCUSSION: This is used for nonisolated asynchronous functions that we
75+
/// want to inherit from their context the context's actor isolation.
76+
CallerIsolationInheriting,
7277
};
7378

7479
private:
@@ -107,6 +112,12 @@ class ActorIsolation {
107112
return ActorIsolation(unsafe ? NonisolatedUnsafe : Nonisolated);
108113
}
109114

115+
static ActorIsolation forCallerIsolationInheriting() {
116+
// NOTE: We do not use parameter indices since the parameter is implicit
117+
// from the perspective of the AST.
118+
return ActorIsolation(CallerIsolationInheriting);
119+
}
120+
110121
static ActorIsolation forActorInstanceSelf(ValueDecl *decl);
111122

112123
/// Create an ActorIsolation appropriate for a type that is self.
@@ -152,8 +163,11 @@ class ActorIsolation {
152163
ActorIsolation::NonisolatedUnsafe))
153164
.Case("global_actor",
154165
std::optional<ActorIsolation>(ActorIsolation::GlobalActor))
155-
.Case("global_actor_unsafe", std::optional<ActorIsolation>(
156-
ActorIsolation::GlobalActor))
166+
.Case("global_actor_unsafe",
167+
std::optional<ActorIsolation>(ActorIsolation::GlobalActor))
168+
.Case("caller_isolation_inheriting",
169+
std::optional<ActorIsolation>(
170+
ActorIsolation::CallerIsolationInheriting))
157171
.Default(std::nullopt);
158172
if (kind == std::nullopt)
159173
return std::nullopt;
@@ -180,8 +194,8 @@ class ActorIsolation {
180194
return parameterIndex;
181195
}
182196

183-
/// Returns true if this actor-instance isolation applies to the self
184-
/// parameter of a method.
197+
/// Returns true if this is an actor-instance isolation that additionally
198+
/// applies to the self parameter of a method.
185199
bool isActorInstanceForSelfParameter() const {
186200
return getActorInstanceParameter() == 0;
187201
}
@@ -198,6 +212,7 @@ class ActorIsolation {
198212
case Unspecified:
199213
case Nonisolated:
200214
case NonisolatedUnsafe:
215+
case CallerIsolationInheriting:
201216
return false;
202217
}
203218
}

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,6 +2940,10 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
29402940
printFlag(true, "dynamically_isolated", CapturesColor);
29412941
break;
29422942

2943+
case ActorIsolation::CallerIsolationInheriting:
2944+
printFlag(true, "isolated_to_caller_isolation", CapturesColor);
2945+
break;
2946+
29432947
case ActorIsolation::ActorInstance:
29442948
printFieldQuoted(isolation.getActorInstance()->printRef(),
29452949
"actor_isolated", CapturesColor);

lib/AST/ActorIsolation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ bool ActorIsolation::isEqual(const ActorIsolation &lhs,
174174
// to answer.
175175
return false;
176176

177+
case CallerIsolationInheriting:
178+
// This returns false for the same reason as erased. The caller has to check
179+
// against the actual caller isolation.
180+
return false;
181+
177182
case ActorInstance: {
178183
auto *lhsActor = lhs.getActorInstance();
179184
auto *rhsActor = rhs.getActorInstance();

lib/AST/Decl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,7 @@ static bool deferMatchesEnclosingAccess(const FuncDecl *defer) {
26882688

26892689
return true;
26902690

2691+
case ActorIsolation::CallerIsolationInheriting:
26912692
case ActorIsolation::ActorInstance:
26922693
case ActorIsolation::Nonisolated:
26932694
case ActorIsolation::Erased: // really can't happen
@@ -11198,6 +11199,7 @@ bool VarDecl::isSelfParamCaptureIsolated() const {
1119811199
case ActorIsolation::NonisolatedUnsafe:
1119911200
case ActorIsolation::GlobalActor:
1120011201
case ActorIsolation::Erased:
11202+
case ActorIsolation::CallerIsolationInheriting:
1120111203
return false;
1120211204

1120311205
case ActorIsolation::ActorInstance:

lib/AST/TypeCheckRequests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,7 @@ SourceLoc MacroDefinitionRequest::getNearestLoc() const {
18561856

18571857
bool ActorIsolation::requiresSubstitution() const {
18581858
switch (kind) {
1859+
case CallerIsolationInheriting:
18591860
case ActorInstance:
18601861
case Nonisolated:
18611862
case NonisolatedUnsafe:
@@ -1871,6 +1872,7 @@ bool ActorIsolation::requiresSubstitution() const {
18711872
ActorIsolation ActorIsolation::subst(SubstitutionMap subs) const {
18721873
switch (kind) {
18731874
case ActorInstance:
1875+
case CallerIsolationInheriting:
18741876
case Nonisolated:
18751877
case NonisolatedUnsafe:
18761878
case Unspecified:
@@ -1891,6 +1893,11 @@ void ActorIsolation::printForDiagnostics(llvm::raw_ostream &os,
18911893
os << "actor" << (asNoun ? " isolation" : "-isolated");
18921894
break;
18931895

1896+
case ActorIsolation::CallerIsolationInheriting:
1897+
os << "caller isolation inheriting"
1898+
<< (asNoun ? " isolation" : "-isolated");
1899+
break;
1900+
18941901
case ActorIsolation::GlobalActor: {
18951902
if (isMainActor()) {
18961903
os << "main actor" << (asNoun ? " isolation" : "-isolated");
@@ -1927,6 +1934,9 @@ void ActorIsolation::print(llvm::raw_ostream &os) const {
19271934
os << ". name: '" << vd->getBaseIdentifier() << "'";
19281935
}
19291936
return;
1937+
case CallerIsolationInheriting:
1938+
os << "caller_isolation_inheriting";
1939+
return;
19301940
case Nonisolated:
19311941
os << "nonisolated";
19321942
return;
@@ -1951,6 +1961,9 @@ void ActorIsolation::printForSIL(llvm::raw_ostream &os) const {
19511961
case ActorInstance:
19521962
os << "actor_instance";
19531963
return;
1964+
case CallerIsolationInheriting:
1965+
os << "caller_isolation_inheriting";
1966+
return;
19541967
case Nonisolated:
19551968
os << "nonisolated";
19561969
return;
@@ -1996,6 +2009,10 @@ void swift::simple_display(
19962009
}
19972010
break;
19982011

2012+
case ActorIsolation::CallerIsolationInheriting:
2013+
out << "isolated to isolation of caller";
2014+
break;
2015+
19992016
case ActorIsolation::Nonisolated:
20002017
case ActorIsolation::NonisolatedUnsafe:
20012018
out << "nonisolated";

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ void CompletionLookup::analyzeActorIsolation(
827827
break;
828828
case ActorIsolation::Unspecified:
829829
case ActorIsolation::Nonisolated:
830+
case ActorIsolation::CallerIsolationInheriting:
830831
case ActorIsolation::NonisolatedUnsafe:
831832
return;
832833
}

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,7 @@ static void emitDelayedArguments(SILGenFunction &SGF,
31043104

31053105
case ActorIsolation::Unspecified:
31063106
case ActorIsolation::Nonisolated:
3107+
case ActorIsolation::CallerIsolationInheriting:
31073108
case ActorIsolation::NonisolatedUnsafe:
31083109
llvm_unreachable("Not isolated");
31093110
}
@@ -5746,6 +5747,7 @@ RValue SILGenFunction::emitApply(
57465747

57475748
case ActorIsolation::Unspecified:
57485749
case ActorIsolation::Nonisolated:
5750+
case ActorIsolation::CallerIsolationInheriting:
57495751
case ActorIsolation::NonisolatedUnsafe:
57505752
llvm_unreachable("Not isolated");
57515753
break;

lib/SILGen/SILGenConcurrency.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void SILGenFunction::emitExpectedExecutorProlog() {
8484
case ActorIsolation::Nonisolated:
8585
case ActorIsolation::NonisolatedUnsafe:
8686
case ActorIsolation::Unspecified:
87+
case ActorIsolation::CallerIsolationInheriting:
8788
return false;
8889

8990
case ActorIsolation::Erased:
@@ -138,6 +139,7 @@ void SILGenFunction::emitExpectedExecutorProlog() {
138139
switch (actorIsolation.getKind()) {
139140
case ActorIsolation::Unspecified:
140141
case ActorIsolation::Nonisolated:
142+
case ActorIsolation::CallerIsolationInheriting:
141143
case ActorIsolation::NonisolatedUnsafe:
142144
break;
143145

@@ -174,6 +176,7 @@ void SILGenFunction::emitExpectedExecutorProlog() {
174176
switch (actorIsolation.getKind()) {
175177
case ActorIsolation::Unspecified:
176178
case ActorIsolation::Nonisolated:
179+
case ActorIsolation::CallerIsolationInheriting:
177180
case ActorIsolation::NonisolatedUnsafe:
178181
break;
179182

@@ -601,6 +604,7 @@ SILGenFunction::emitClosureIsolation(SILLocation loc, SILDeclRef constant,
601604
switch (isolation) {
602605
case ActorIsolation::Unspecified:
603606
case ActorIsolation::Nonisolated:
607+
case ActorIsolation::CallerIsolationInheriting:
604608
case ActorIsolation::NonisolatedUnsafe:
605609
return emitNonIsolatedIsolation(loc);
606610

@@ -652,6 +656,7 @@ SILGenFunction::emitExecutor(SILLocation loc, ActorIsolation isolation,
652656
switch (isolation.getKind()) {
653657
case ActorIsolation::Unspecified:
654658
case ActorIsolation::Nonisolated:
659+
case ActorIsolation::CallerIsolationInheriting:
655660
case ActorIsolation::NonisolatedUnsafe:
656661
return std::nullopt;
657662

lib/SILGen/SILGenConstructor.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,17 +594,18 @@ static bool ctorHopsInjectedByDefiniteInit(ConstructorDecl *ctor,
594594

595595
// must be self-isolated
596596
switch (isolation) {
597-
case ActorIsolation::ActorInstance:
598-
return isolation.getActorInstanceParameter() == 0;
597+
case ActorIsolation::ActorInstance:
598+
return isolation.getActorInstanceParameter() == 0;
599599

600-
case ActorIsolation::Erased:
601-
llvm_unreachable("constructor cannot have erased isolation");
600+
case ActorIsolation::Erased:
601+
llvm_unreachable("constructor cannot have erased isolation");
602602

603-
case ActorIsolation::Unspecified:
604-
case ActorIsolation::Nonisolated:
605-
case ActorIsolation::NonisolatedUnsafe:
606-
case ActorIsolation::GlobalActor:
607-
return false;
603+
case ActorIsolation::Unspecified:
604+
case ActorIsolation::Nonisolated:
605+
case ActorIsolation::NonisolatedUnsafe:
606+
case ActorIsolation::GlobalActor:
607+
case ActorIsolation::CallerIsolationInheriting:
608+
return false;
608609
}
609610
}
610611

@@ -1558,6 +1559,7 @@ void SILGenFunction::emitMemberInitializer(DeclContext *dc, VarDecl *selfDecl,
15581559
case ActorIsolation::Unspecified:
15591560
case ActorIsolation::Nonisolated:
15601561
case ActorIsolation::NonisolatedUnsafe:
1562+
case ActorIsolation::CallerIsolationInheriting:
15611563
break;
15621564

15631565
case ActorIsolation::Erased:

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
10971097
switch (actorIsolation) {
10981098
case ActorIsolation::Unspecified:
10991099
case ActorIsolation::Nonisolated:
1100+
case ActorIsolation::CallerIsolationInheriting:
11001101
case ActorIsolation::NonisolatedUnsafe:
11011102
case ActorIsolation::ActorInstance:
11021103
break;

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ void LifetimeChecker::injectActorHops() {
10671067

10681068
case ActorIsolation::Unspecified:
10691069
case ActorIsolation::Nonisolated:
1070+
case ActorIsolation::CallerIsolationInheriting:
10701071
case ActorIsolation::NonisolatedUnsafe:
10711072
case ActorIsolation::GlobalActor:
10721073
return;

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,6 +5093,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
50935093
switch (getActorIsolation(storage)) {
50945094
case ActorIsolation::Unspecified:
50955095
case ActorIsolation::Nonisolated:
5096+
case ActorIsolation::CallerIsolationInheriting:
50965097
case ActorIsolation::NonisolatedUnsafe:
50975098
break;
50985099

0 commit comments

Comments
 (0)