Skip to content

Commit d9aa869

Browse files
committed
[Concurrency] Teach the constraint system about .isolation on dynamically
isolated function values.
1 parent 78384d5 commit d9aa869

File tree

9 files changed

+52
-1
lines changed

9 files changed

+52
-1
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ class ExtractFunctionIsolationExpr : public Expr {
35303530

35313531
public:
35323532
ExtractFunctionIsolationExpr(Expr *fnExpr, SourceLoc isolationLoc,
3533-
Type type, bool implicit)
3533+
Type type, bool implicit = false)
35343534
: Expr(ExprKind::ExtractFunctionIsolation, implicit, type),
35353535
fnExpr(fnExpr), isolationLoc(isolationLoc) {}
35363536

include/swift/Sema/OverloadChoice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ enum class OverloadChoiceKind : int {
5656
DeclViaUnwrappedOptional,
5757
/// The overload choice materializes a pack from a tuple.
5858
MaterializePack,
59+
/// The overload choice extracts the isolation of a dynamically isolated
60+
/// function value.
61+
ExtractFunctionIsolation,
5962
/// The overload choice indexes into a tuple. Index zero will
6063
/// have the value of this enumerator, index one will have the value of this
6164
/// enumerator + 1, and so on. Thus, this enumerator must always be last.

lib/IDE/SelectedOverloadInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ swift::ide::getSelectedOverloadInfo(const Solution &S,
8989
case OverloadChoiceKind::DynamicMemberLookup:
9090
case OverloadChoiceKind::TupleIndex:
9191
case OverloadChoiceKind::MaterializePack:
92+
case OverloadChoiceKind::ExtractFunctionIsolation:
9293
// If it's DynamicMemberLookup, we don't know which function is being
9394
// called, so we can't extract any information from it.
9495
// TupleIndex isn't a function call and is not relevant for argument

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,15 @@ namespace {
34233423
packType));
34243424
}
34253425

3426+
case OverloadChoiceKind::ExtractFunctionIsolation: {
3427+
auto isolationType = solution.getResolvedType(expr);
3428+
auto *extractExpr = new (cs.getASTContext())
3429+
ExtractFunctionIsolationExpr(base,
3430+
expr->getEndLoc(),
3431+
isolationType);
3432+
return cs.cacheType(extractExpr);
3433+
}
3434+
34263435
case OverloadChoiceKind::KeyPathApplication:
34273436
llvm_unreachable("should only happen in a subscript");
34283437

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ static bool sameOverloadChoice(const OverloadChoice &x,
205205
return x.getTupleIndex() == y.getTupleIndex();
206206

207207
case OverloadChoiceKind::MaterializePack:
208+
case OverloadChoiceKind::ExtractFunctionIsolation:
208209
return true;
209210
}
210211

@@ -1121,6 +1122,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
11211122
switch (choice1.getKind()) {
11221123
case OverloadChoiceKind::TupleIndex:
11231124
case OverloadChoiceKind::MaterializePack:
1125+
case OverloadChoiceKind::ExtractFunctionIsolation:
11241126
continue;
11251127

11261128
case OverloadChoiceKind::KeyPathApplication:

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ static bool areConservativelyCompatibleArgumentLabels(
205205
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
206206
case OverloadChoiceKind::TupleIndex:
207207
case OverloadChoiceKind::MaterializePack:
208+
case OverloadChoiceKind::ExtractFunctionIsolation:
208209
return true;
209210
}
210211

@@ -9746,6 +9747,16 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
97469747
if (auto *selfTy = instanceTy->getAs<DynamicSelfType>())
97479748
instanceTy = selfTy->getSelfType();
97489749

9750+
// Dynamically isolated function types have a magic '.isolation'
9751+
// member that extracts the isolation value.
9752+
if (auto *fn = dyn_cast<FunctionType>(instanceTy)) {
9753+
if (fn->getIsolation().isErased() &&
9754+
memberName.getBaseIdentifier().str() == "isolation") {
9755+
result.ViableCandidates.push_back(
9756+
OverloadChoice(baseTy, OverloadChoiceKind::ExtractFunctionIsolation));
9757+
}
9758+
}
9759+
97499760
if (!instanceTy->mayHaveMembers())
97509761
return result;
97519762

lib/Sema/Constraint.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm,
534534
case OverloadChoiceKind::MaterializePack:
535535
Out << "materialize pack";
536536
break;
537+
case OverloadChoiceKind::ExtractFunctionIsolation:
538+
Out << "extract function islation";
539+
break;
537540
case OverloadChoiceKind::KeyPathApplication:
538541
Out << "key path application";
539542
break;

lib/Sema/ConstraintSystem.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,7 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
28902890
case OverloadChoiceKind::KeyPathApplication:
28912891
case OverloadChoiceKind::TupleIndex:
28922892
case OverloadChoiceKind::MaterializePack:
2893+
case OverloadChoiceKind::ExtractFunctionIsolation:
28932894
return Type();
28942895
}
28952896

@@ -3569,6 +3570,7 @@ void ConstraintSystem::bindOverloadType(
35693570
case OverloadChoiceKind::DeclViaUnwrappedOptional:
35703571
case OverloadChoiceKind::TupleIndex:
35713572
case OverloadChoiceKind::MaterializePack:
3573+
case OverloadChoiceKind::ExtractFunctionIsolation:
35723574
case OverloadChoiceKind::KeyPathApplication:
35733575
bindTypeOrIUO(openedType);
35743576
return;
@@ -3829,6 +3831,15 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
38293831
break;
38303832
}
38313833

3834+
case OverloadChoiceKind::ExtractFunctionIsolation: {
3835+
// The type of `.isolation` is `(any Actor)?`
3836+
auto actor = getASTContext().getProtocol(KnownProtocolKind::Actor);
3837+
adjustedRefType =
3838+
OptionalType::get(actor->getDeclaredExistentialType());
3839+
refType = adjustedRefType;
3840+
break;
3841+
}
3842+
38323843
case OverloadChoiceKind::KeyPathApplication: {
38333844
// Key path application looks like a subscript(keyPath: KeyPath<Base, T>).
38343845
// The element type is T or @lvalue T based on the key path subtype and
@@ -4466,6 +4477,7 @@ DeclName OverloadChoice::getName() const {
44664477

44674478
case OverloadChoiceKind::MaterializePack:
44684479
case OverloadChoiceKind::TupleIndex:
4480+
case OverloadChoiceKind::ExtractFunctionIsolation:
44694481
llvm_unreachable("no name!");
44704482
}
44714483

@@ -5830,6 +5842,7 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
58305842

58315843
case OverloadChoiceKind::TupleIndex:
58325844
case OverloadChoiceKind::MaterializePack:
5845+
case OverloadChoiceKind::ExtractFunctionIsolation:
58335846
// FIXME: Actually diagnose something here.
58345847
break;
58355848
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,10 @@ void OverloadChoice::dump(Type adjustedOpenedType, SourceManager *sm,
13041304
case OverloadChoiceKind::MaterializePack:
13051305
out << "materialize pack from tuple " << getBaseType()->getString(PO);
13061306
break;
1307+
1308+
case OverloadChoiceKind::ExtractFunctionIsolation:
1309+
out << "extract isolation from " << getBaseType()->getString(PO);
1310+
break;
13071311
}
13081312
}
13091313

@@ -1610,6 +1614,11 @@ void ConstraintSystem::print(raw_ostream &out) const {
16101614
out << "materialize pack from tuple "
16111615
<< choice.getBaseType()->getString(PO);
16121616
break;
1617+
1618+
case OverloadChoiceKind::ExtractFunctionIsolation:
1619+
out << "extract isolation from "
1620+
<< choice.getBaseType()->getString(PO);
1621+
break;
16131622
}
16141623
out << " for ";
16151624
elt.first->dump(&getASTContext().SourceMgr, out);

0 commit comments

Comments
 (0)