Skip to content

Commit 68baadc

Browse files
committed
[SILGen] Emit runtime actor isolation checks for @objc thunks of closures
1 parent b5daa70 commit 68baadc

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,15 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
15971597
getASTContext().LangOpts.hasFeature(Feature::PreconcurrencyConformances);
15981598

15991599
llvm::Optional<ActorIsolation> isolation;
1600-
if ((F.isAsync() || emitExecutorPrecondition) && thunk.hasDecl()) {
1601-
isolation = getActorIsolation(thunk.getDecl());
1600+
if (F.isAsync()) {
1601+
if (thunk.hasDecl())
1602+
isolation = getActorIsolation(thunk.getDecl());
1603+
} else if (emitExecutorPrecondition) {
1604+
if (thunk.hasDecl()) {
1605+
isolation = getActorIsolation(thunk.getDecl());
1606+
} else if (auto globalActor = nativeInfo.FormalType->getGlobalActor()) {
1607+
isolation = ActorIsolation::forGlobalActor(globalActor);
1608+
}
16021609
}
16031610

16041611
// A hop/check is only needed in the thunk if it is global-actor isolated.

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7442,10 +7442,15 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
74427442
if (!(overload && overload->choice.isDecl()))
74437443
return false;
74447444

7445-
auto *overloadModule = overload->choice.getDecl()->getModuleContext();
7446-
return overloadModule != dc->getParentModule() &&
7447-
!overloadModule->getLanguageVersionBuiltWith()
7448-
.isVersionAtLeast(6);
7445+
auto *decl = overload->choice.getDecl();
7446+
// Function values passed to C/ObjC APIs are already thunked
7447+
// and that's where the check is going to go.
7448+
if (decl->hasClangNode())
7449+
return false;
7450+
7451+
auto *declaredIn = decl->getModuleContext();
7452+
return declaredIn && declaredIn != dc->getParentModule() &&
7453+
!declaredIn->getLanguageVersionBuiltWith().isVersionAtLeast(6);
74497454
};
74507455

74517456
if (requiresRuntimeCheck()) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-feature PreconcurrencyConformances -emit-silgen -verify %s | %FileCheck %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
install_global_event_handler { @MainActor _ in
7+
// expected-warning@-1 {{converting function value of type '@MainActor (Any) -> ()' to 'event_handler' (aka '@convention(c) (Any) -> ()') loses global actor 'MainActor'; this is an error in Swift 6}}
8+
}
9+
10+
// CHECK-LABEL: sil private [thunk] [ossa] @$s56dynamic_checks_for_func_refs_in_preconcurrency_apis_objcyypScMYccfU_To : $@convention(c) (AnyObject) -> ()
11+
// CHECK: [[MAIN_ACTOR_METATYPE:%.*]] = metatype $@thick MainActor.Type
12+
// CHECK: [[SHARED_FIELD_GETTER:%.*]] = function_ref @$sScM6sharedScMvgZ : $@convention(method) (@thick MainActor.Type) -> @owned MainActor
13+
// CHECK-NEXT: [[MAIN_ACTOR:%.*]] = apply [[SHARED_FIELD_GETTER]]([[MAIN_ACTOR_METATYPE]]) : $@convention(method) (@thick MainActor.Type) -> @owned MainActor
14+
// CHECK-NEXT: [[MAIN_ACTOR_ACCESS:%.*]] = begin_borrow [[MAIN_ACTOR]] : $MainActor
15+
// CHECK-NEXT: [[EXEC:%.*]] = extract_executor [[MAIN_ACTOR_ACCESS]] : $MainActor
16+
// CHECK: [[CHECK_EXEC_REF:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF
17+
// CHECK-NEXT: {{.*}} = apply [[CHECK_EXEC_REF]]({{.*}}, [[EXEC]])

0 commit comments

Comments
 (0)