Skip to content

Commit a4d9b3b

Browse files
committed
[AST] Add a bit to closure expr to indiciate whether it requires dynamic isolation checking
This is an important information for closures because the compiler might need to emit dynamic actor isolation checks in some circumstances (i.e. when a closure is isolated and passed to a not fully concurrency checked API).
1 parent 911933e commit a4d9b3b

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

include/swift/AST/Expr.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
266266
Kind : 2
267267
);
268268

269-
SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1+1+1+1,
269+
SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1+1+1+1+1,
270270
/// True if closure parameters were synthesized from anonymous closure
271271
/// variables.
272272
HasAnonymousClosureVars : 1,
@@ -288,7 +288,13 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
288288

289289
/// True if we're in the common case where the GlobalActorAttributeRequest
290290
/// request returned a pair of null pointers.
291-
NoGlobalActorAttribute : 1
291+
NoGlobalActorAttribute : 1,
292+
293+
/// Indicates whether this closure literal would require dynamic actor
294+
/// isolation checks when it either specifies or inherits isolation
295+
/// and was passed as an argument to a function that is not fully
296+
/// concurrency checked.
297+
RequiresDynamicIsolationChecking : 1
292298
);
293299

294300
SWIFT_INLINE_BITFIELD_FULL(BindOptionalExpr, Expr, 16,
@@ -4158,6 +4164,7 @@ class ClosureExpr : public AbstractClosureExpr {
41584164
Bits.ClosureExpr.InheritActorContext = false;
41594165
Bits.ClosureExpr.IsPassedToSendingParameter = false;
41604166
Bits.ClosureExpr.NoGlobalActorAttribute = false;
4167+
Bits.ClosureExpr.RequiresDynamicIsolationChecking = false;
41614168
}
41624169

41634170
SourceRange getSourceRange() const;
@@ -4223,6 +4230,16 @@ class ClosureExpr : public AbstractClosureExpr {
42234230
Bits.ClosureExpr.IsPassedToSendingParameter = value;
42244231
}
42254232

4233+
/// True if this is an isolated closure literal that is passed
4234+
/// to a callee that has not been concurrency checked.
4235+
bool requiresDynamicIsolationChecking() const {
4236+
return Bits.ClosureExpr.RequiresDynamicIsolationChecking;
4237+
}
4238+
4239+
void setRequiresDynamicIsolationChecking(bool value = true) {
4240+
Bits.ClosureExpr.RequiresDynamicIsolationChecking = value;
4241+
}
4242+
42264243
/// Determine whether this closure expression has an
42274244
/// explicitly-specified result type.
42284245
bool hasExplicitResultType() const { return ArrowLoc.isValid(); }

include/swift/AST/Types.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3868,9 +3868,6 @@ struct ParameterListInfo {
38683868
/// actor context from the context in which it was created.
38693869
bool inheritsActorContext(unsigned paramIdx) const;
38703870

3871-
/// Whether there is any contextual information set on this parameter list.
3872-
bool anyContextualInfo() const;
3873-
38743871
bool isVariadicGenericParameter(unsigned paramIdx) const;
38753872

38763873
/// Returns true if this is a sending parameter.

lib/AST/Type.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,6 @@ bool ParameterListInfo::inheritsActorContext(unsigned paramIdx) const {
13781378
: false;
13791379
}
13801380

1381-
bool ParameterListInfo::anyContextualInfo() const {
1382-
return implicitSelfCapture.any() || inheritActorContext.any();
1383-
}
1384-
13851381
bool ParameterListInfo::isVariadicGenericParameter(unsigned paramIdx) const {
13861382
return paramIdx < variadicGenerics.size()
13871383
? variadicGenerics[paramIdx]

lib/Sema/CSApply.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,10 +6084,22 @@ ArgumentList *ExprRewriter::coerceCallArguments(
60846084
return placeholder;
60856085
};
60866086

6087+
auto applyFlagsToArgument = [&paramInfo](unsigned paramIdx, Expr *argument) {
6088+
bool isImplicitSelfCapture = paramInfo.isImplicitSelfCapture(paramIdx);
6089+
bool inheritsActorContext = paramInfo.inheritsActorContext(paramIdx);
6090+
bool isPassedToSendingParameter = paramInfo.isSendingParameter(paramIdx);
6091+
6092+
applyContextualClosureFlags(argument, isImplicitSelfCapture,
6093+
inheritsActorContext,
6094+
isPassedToSendingParameter);
6095+
};
6096+
60876097
// Quickly test if any further fix-ups for the argument types are necessary.
60886098
auto matches = args->matches(params, [&](Expr *E) { return cs.getType(E); });
6089-
if (matches && !shouldInjectWrappedValuePlaceholder &&
6090-
!paramInfo.anyContextualInfo()) {
6099+
if (matches && !shouldInjectWrappedValuePlaceholder) {
6100+
for (unsigned paramIdx : indices(params)) {
6101+
applyFlagsToArgument(paramIdx, args->getExpr(paramIdx));
6102+
}
60916103
return args;
60926104
}
60936105

@@ -6204,15 +6216,10 @@ ArgumentList *ExprRewriter::coerceCallArguments(
62046216
// for things like trailing closures and args to property wrapper params.
62056217
arg.setLabel(param.getLabel());
62066218

6207-
// Determine whether the closure argument should be treated as having
6208-
// implicit self capture or inheriting actor context.
6209-
bool isImplicitSelfCapture = paramInfo.isImplicitSelfCapture(paramIdx);
6210-
bool inheritsActorContext = paramInfo.inheritsActorContext(paramIdx);
6211-
bool isPassedToSendingParameter = paramInfo.isSendingParameter(paramIdx);
6212-
6213-
applyContextualClosureFlags(argExpr, isImplicitSelfCapture,
6214-
inheritsActorContext,
6215-
isPassedToSendingParameter);
6219+
// Determine whether the argument should be marked as having
6220+
// implicit self capture, inheriting actor context, is passed to a
6221+
// `sending` parameter etc.
6222+
applyFlagsToArgument(paramIdx, argExpr);
62166223

62176224
// If the types exactly match, this is easy.
62186225
auto paramType = param.getOldType();

0 commit comments

Comments
 (0)