Skip to content

Commit b79e922

Browse files
committed
[CSApply] Don't inject global actor into closure type if it's marked as @concurrent
When the attribute is specified explicitly passing a `@concurrent` closure to a global actor-isolated parameter or contextual type should result in a conversion and closure itself should be nonisolated. Resolves: rdar://151797372 (cherry picked from commit efc6efc)
1 parent 3db1314 commit b79e922

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/Sema/CSApply.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7749,11 +7749,13 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
77497749
}
77507750

77517751
// If we have a ClosureExpr, then we can safely propagate a global actor
7752-
// to the closure without invalidating prior analysis.
7752+
// to the closure if it's not explicitly marked as `@concurrent` without
7753+
// invalidating prior analysis.
77537754
fromEI = fromFunc->getExtInfo();
7754-
if (toEI.getGlobalActor() && !fromEI.getGlobalActor()) {
7755-
auto newFromFuncType = fromFunc->withExtInfo(
7756-
fromEI.withGlobalActor(toEI.getGlobalActor()));
7755+
if (toEI.getGlobalActor() && !fromEI.getGlobalActor() &&
7756+
!isClosureMarkedAsConcurrent(expr)) {
7757+
auto newFromFuncType =
7758+
fromFunc->withExtInfo(fromEI.withGlobalActor(toEI.getGlobalActor()));
77577759
if (applyTypeToClosureExpr(cs, expr, newFromFuncType)) {
77587760
fromFunc = newFromFuncType->castTo<FunctionType>();
77597761

test/Concurrency/attr_execution/conversions_silgen.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,14 @@ func testNoIsolationTransfer() {
462462
// CHECK: hop_to_executor [[GENERIC_EXECUTOR]]
463463
testErasure { @concurrent in }
464464
}
465+
466+
func testClosuresDontAssumeGlobalActorWithMarkedAsConcurrent() {
467+
func test(_ fn: @MainActor () async -> Void) {}
468+
469+
// CHECK-LABEL: sil private [ossa] @$s21attr_execution_silgen55testClosuresDontAssumeGlobalActorWithMarkedAsConcurrentyyFyyYaYbXEfU_
470+
// CHECK: [[GENERIC_EXECUTOR:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none!enumelt
471+
// CHECK-NEXT: hop_to_executor [[GENERIC_EXECUTOR]]
472+
// CHECK: } // end sil function '$s21attr_execution_silgen55testClosuresDontAssumeGlobalActorWithMarkedAsConcurrentyyFyyYaYbXEfU_'
473+
test { @Sendable @concurrent in
474+
}
475+
}

0 commit comments

Comments
 (0)