Skip to content

Commit 0a627bc

Browse files
authored
Merge pull request swiftlang#72324 from hborla/extract-function-isolation-expr
[Concurrency] Add a `.isolation` member on dynamically isolated function values.
2 parents 9fa0028 + f5e407e commit 0a627bc

18 files changed

+160
-0
lines changed

include/swift/AST/Expr.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,43 @@ class ActorIsolationErasureExpr : public ImplicitConversionExpr {
35183518
}
35193519
};
35203520

3521+
/// Extracts the isolation of a dynamically isolated function value.
3522+
class ExtractFunctionIsolationExpr : public Expr {
3523+
/// The function value expression from which to extract the
3524+
/// isolation. The type of `fnExpr` must be an ``@isolated(any)`
3525+
/// funciton.
3526+
Expr *fnExpr;
3527+
3528+
/// The source location of `.isolation`
3529+
SourceLoc isolationLoc;
3530+
3531+
public:
3532+
ExtractFunctionIsolationExpr(Expr *fnExpr, SourceLoc isolationLoc,
3533+
Type type, bool implicit = false)
3534+
: Expr(ExprKind::ExtractFunctionIsolation, implicit, type),
3535+
fnExpr(fnExpr), isolationLoc(isolationLoc) {}
3536+
3537+
Expr *getFunctionExpr() const {
3538+
return fnExpr;
3539+
}
3540+
3541+
void setFunctionExpr(Expr *fnExpr) {
3542+
this->fnExpr = fnExpr;
3543+
}
3544+
3545+
SourceLoc getStartLoc() const {
3546+
return fnExpr->getStartLoc();
3547+
}
3548+
3549+
SourceLoc getEndLoc() const {
3550+
return isolationLoc;
3551+
}
3552+
3553+
static bool classof(const Expr *E) {
3554+
return E->getKind() == ExprKind::ExtractFunctionIsolation;
3555+
}
3556+
};
3557+
35213558
/// UnresolvedSpecializeExpr - Represents an explicit specialization using
35223559
/// a type parameter list (e.g. "Vector<Int>") that has not been resolved.
35233560
class UnresolvedSpecializeExpr final : public Expr,

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ EXPR(VarargExpansion, Expr)
134134
EXPR(PackExpansion, Expr)
135135
EXPR(PackElement, Expr)
136136
EXPR(MaterializePack, Expr)
137+
EXPR(ExtractFunctionIsolation, Expr)
137138
EXPR(DynamicType, Expr)
138139
EXPR(RebindSelfInConstructor, Expr)
139140
EXPR(OpaqueValue, Expr)

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/AST/ASTDumper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,13 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
26992699
printFoot();
27002700
}
27012701

2702+
void visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
2703+
StringRef label) {
2704+
printCommon(E, "extract_function_isolation", label);
2705+
printRec(E->getFunctionExpr());
2706+
printFoot();
2707+
}
2708+
27022709
void visitInOutExpr(InOutExpr *E, StringRef label) {
27032710
printCommon(E, "inout_expr", label);
27042711
printRec(E->getSubExpr());

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5290,6 +5290,11 @@ void PrintAST::visitLinearToDifferentiableFunctionExpr(swift::LinearToDifferenti
52905290
void PrintAST::visitActorIsolationErasureExpr(ActorIsolationErasureExpr *expr) {
52915291
}
52925292

5293+
void PrintAST::visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *expr) {
5294+
visit(expr->getFunctionExpr());
5295+
Printer << ".isolation";
5296+
}
5297+
52935298
void PrintAST::visitPropertyWrapperValuePlaceholderExpr(swift::PropertyWrapperValuePlaceholderExpr *expr) {
52945299
}
52955300

lib/AST/ASTWalker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,17 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13261326
return E;
13271327
}
13281328

1329+
Expr *visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
1330+
if (auto fn = E->getFunctionExpr()) {
1331+
if (auto newFn = doIt(fn))
1332+
E->setFunctionExpr(newFn);
1333+
else
1334+
return nullptr;
1335+
}
1336+
1337+
return E;
1338+
}
1339+
13291340
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) { return E; }
13301341

13311342
Expr *visitSingleValueStmtExpr(SingleValueStmtExpr *E) {

lib/AST/Expr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
378378
PASS_THROUGH_REFERENCE(Try, getSubExpr);
379379
PASS_THROUGH_REFERENCE(ForceTry, getSubExpr);
380380
PASS_THROUGH_REFERENCE(OptionalTry, getSubExpr);
381+
PASS_THROUGH_REFERENCE(ExtractFunctionIsolation, getFunctionExpr);
381382

382383
NO_REFERENCE(Tuple);
383384
SIMPLE_REFERENCE(Array, getInitializer);
@@ -723,6 +724,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
723724
case ExprKind::UnresolvedSpecialize:
724725
case ExprKind::UnresolvedMember:
725726
case ExprKind::UnresolvedDot:
727+
case ExprKind::ExtractFunctionIsolation:
726728
return true;
727729

728730
case ExprKind::Sequence:
@@ -1032,6 +1034,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
10321034
case ExprKind::MacroExpansion:
10331035
case ExprKind::CurrentContextIsolation:
10341036
case ExprKind::ActorIsolationErasure:
1037+
case ExprKind::ExtractFunctionIsolation:
10351038
return false;
10361039
}
10371040

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/SILGen/SILGenExpr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ namespace {
486486
SGFContext C);
487487
RValue visitActorIsolationErasureExpr(ActorIsolationErasureExpr *E,
488488
SGFContext C);
489+
RValue visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
490+
SGFContext C);
489491
RValue visitCovariantFunctionConversionExpr(
490492
CovariantFunctionConversionExpr *E,
491493
SGFContext C);
@@ -2107,6 +2109,14 @@ RValue RValueEmitter::visitActorIsolationErasureExpr(ActorIsolationErasureExpr *
21072109
nonIsolatedType));
21082110
}
21092111

2112+
RValue RValueEmitter::visitExtractFunctionIsolationExpr(
2113+
ExtractFunctionIsolationExpr *E, SGFContext C) {
2114+
auto arg = SGF.emitRValue(E->getFunctionExpr());
2115+
auto result = SGF.emitExtractFunctionIsolation(
2116+
E, ArgumentSource(E, std::move(arg)), C);
2117+
return RValue(SGF, E, result);
2118+
}
2119+
21102120
RValue RValueEmitter::visitErasureExpr(ErasureExpr *E, SGFContext C) {
21112121
if (auto result = tryEmitAsBridgingConversion(SGF, E, false, C)) {
21122122
return RValue(SGF, E, *result);

lib/Sema/CSApply.cpp

Lines changed: 13 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

@@ -5372,6 +5381,10 @@ namespace {
53725381
return E;
53735382
}
53745383

5384+
Expr *visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
5385+
llvm_unreachable("found ExtractFunctionIsolationExpr in CSApply");
5386+
}
5387+
53755388
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) {
53765389
llvm_unreachable("found KeyPathDotExpr in CSApply");
53775390
}

0 commit comments

Comments
 (0)