Skip to content

[Concurrency] Fix @_inheritActorContext(always) to use `forActorIns… #81543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4781,13 +4781,18 @@ ActorIsolation ActorIsolationChecker::determineClosureIsolation(

case ActorIsolation::ActorInstance: {
if (checkIsolatedCapture) {
if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
return ActorIsolation::forActorInstanceCapture(param);

auto *explicitClosure = dyn_cast<ClosureExpr>(closure);
// @_inheritActorContext(always) forces the isolation capture.
if (explicitClosure && explicitClosure->alwaysInheritsActorContext())
if (explicitClosure && explicitClosure->alwaysInheritsActorContext()) {
if (parentIsolation.isActorInstanceIsolated()) {
if (auto *param = parentIsolation.getActorInstance())
return ActorIsolation::forActorInstanceCapture(param);
}
return parentIsolation;

if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
return ActorIsolation::forActorInstanceCapture(param);
}
} else {
// If we don't have capture information during code completion, assume
// that the closure captures the `isolated` parameter from the parent
Expand Down
17 changes: 17 additions & 0 deletions test/SILGen/hop_to_executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func testGlobalActorFuncValue(_ fn: @RedActor () -> Void) async {

func acceptAsyncSendableClosureInheriting<T>(@_inheritActorContext _: @Sendable () async -> T) { }
func acceptAsyncSendableClosureAlwaysInheriting<T>(@_inheritActorContext(always) _: @Sendable () async -> T) { }
func acceptAsyncSendableClosureAlwaysInheritingErased<T>(@_inheritActorContext(always) _: sending @isolated(any) () async -> T) { }

extension MyActor {
func synchronous() { }
Expand Down Expand Up @@ -385,6 +386,22 @@ func testIsolatedParameterWithInheritActorContext(_ isolation: isolated (any Act
}
}

// CHECK-LABEL: sil hidden [ossa] @$s4test0a17IsolatedParamWithB3AnyyyScA_pYiF
// CHECK: [[CLOSURE_REF:%.*]] = function_ref @$s4test0a17IsolatedParamWithB3AnyyyScA_pYiFyyYaXEfU_
// CHECK-NEXT: [[ISOLATION_COPY_1:%.*]] = copy_value %0 : $any Actor
// CHECK-NEXT: [[ISOLATION_COPY_2:%.*]] = copy_value [[ISOLATION_COPY_1]] : $any Actor
// CHECK-NEXT: [[DYNAMIC_ISOLATION:%.*]] = enum $Optional<any Actor>, #Optional.some!enumelt, [[ISOLATION_COPY_2]] : $any Actor
// CHECK-NEXT: [[CLOSURE_WITH_APPLIED_ISOLATION:%.*]] = partial_apply [callee_guaranteed] [isolated_any] [[CLOSURE_REF]]([[DYNAMIC_ISOLATION]], [[ISOLATION_COPY_1]])
func testIsolatedParamWithIsolatedAny(_ isolation: isolated any Actor) {
// CHECK-LABEL: sil private [ossa] @$s4test0a17IsolatedParamWithB3AnyyyScA_pYiFyyYaXEfU_
// CHECK: bb0(%0 : $*(), [[DYNAMIC_ISOLATION:%[0-9]+]] : @guaranteed $Optional<any Actor>, [[CAPTURED_ISOLATION:%[0-9]+]] : @closureCapture @guaranteed $any Actor):
// CHECK: [[COPY:%[0-9]+]] = copy_value [[CAPTURED_ISOLATION]] : $any Actor
// CHECK-NEXT: [[BORROW:%[0-9]+]] = begin_borrow [[COPY]] : $any Actor
// CHECK-NEXT: hop_to_executor [[BORROW]] : $any Actor
acceptAsyncSendableClosureAlwaysInheritingErased {
}
}

func acceptAsyncClosure(_: () async -> Void) { }
func acceptAsyncClosure2<T>(_: (T) async -> T) { }

Expand Down