Skip to content

Commit c0fae83

Browse files
authored
[SILGen] Fix a bug where the wrong convention was being used for computing the type of a closure thunk (#73336)
The ObjC selector family convention was being used instead of the C function type convention. rdar://127090209 (cherry picked from commit 428fe21)
1 parent c45bd72 commit c0fae83

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,6 +4069,19 @@ static CanSILFunctionType getUncachedSILFunctionTypeForConstant(
40694069
}
40704070
}
40714071

4072+
// The type of the native-to-foreign thunk for a swift closure.
4073+
if (constant.isForeign && constant.hasClosureExpr() &&
4074+
shouldStoreClangType(TC.getDeclRefRepresentation(constant))) {
4075+
auto clangType = TC.Context.getClangFunctionType(
4076+
origLoweredInterfaceType->getParams(),
4077+
origLoweredInterfaceType->getResult(),
4078+
FunctionTypeRepresentation::CFunctionPointer);
4079+
AbstractionPattern pattern =
4080+
AbstractionPattern(origLoweredInterfaceType, clangType);
4081+
return getSILFunctionTypeForAbstractCFunction(
4082+
TC, pattern, origLoweredInterfaceType, extInfoBuilder, constant);
4083+
}
4084+
40724085
// If the decl belongs to an ObjC method family, use that family's
40734086
// ownership conventions.
40744087
return getSILFunctionTypeForObjCSelectorFamily(

test/Interop/Cxx/class/Inputs/closure.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ void cfunc2(void (*fp)(NonTrivial)) {
1212
(*fp)(NonTrivial());
1313
}
1414

15+
#if __OBJC__
16+
struct ARCStrong {
17+
id a;
18+
};
19+
20+
void cfuncARCStrong(void (*_Nonnull)(ARCStrong));
21+
#endif
22+
1523
#endif // __CLOSURE__
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-swiftxx-frontend -I %S/Inputs -emit-silgen %s | %FileCheck %s
2+
3+
// REQUIRES: OS=macosx
4+
5+
import Closure
6+
7+
// CHECK: sil [ossa] @$s4main20testClosureToFuncPtryyF : $@convention(thin) () -> () {
8+
// CHECK: %[[V0:.*]] = function_ref @$s4main20testClosureToFuncPtryyFySo9ARCStrongVcfU_To : $@convention(c) (@owned ARCStrong) -> ()
9+
// CHECK: %[[V1:.*]] = function_ref @_Z14cfuncARCStrongPFv9ARCStrongE : $@convention(c) (@convention(c) (@owned ARCStrong) -> ()) -> ()
10+
// CHECK: apply %[[V1]](%[[V0]]) : $@convention(c) (@convention(c) (@owned ARCStrong) -> ()) -> ()
11+
12+
// CHECK: sil private [thunk] [ossa] @$s4main20testClosureToFuncPtryyFySo9ARCStrongVcfU_To : $@convention(c) (@owned ARCStrong) -> () {
13+
// CHECK: bb0(%[[V0:.*]] : @owned $ARCStrong):
14+
// CHECK: %[[V1:.*]] = begin_borrow %[[V0]] : $ARCStrong
15+
// CHECK: %[[V2:.*]] = function_ref @$s4main20testClosureToFuncPtryyFySo9ARCStrongVcfU_ : $@convention(thin) (@guaranteed ARCStrong) -> ()
16+
// CHECK: apply %[[V2]](%[[V1]]) : $@convention(thin) (@guaranteed ARCStrong) -> ()
17+
// CHECK: end_borrow %[[V1]] : $ARCStrong
18+
// CHECK: destroy_value %[[V0]] : $ARCStrong
19+
20+
public func testClosureToFuncPtr() {
21+
cfuncARCStrong({N in})
22+
}

0 commit comments

Comments
 (0)