Skip to content

Commit b107a5e

Browse files
authored
Merge pull request #73436 from jckarter/c-function-pointer-conversion-sendable
Look through `@Sendable` conversions when considering C function pointer conversions.
2 parents c88e65b + d6e4b6b commit b107a5e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,19 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
17781778
loc = declRef.getDecl();
17791779
};
17801780

1781+
if (auto conv = dyn_cast<FunctionConversionExpr>(semanticExpr)) {
1782+
// There might be an intermediate conversion adding or removing @Sendable.
1783+
#ifndef NDEBUG
1784+
{
1785+
auto ty1 = conv->getType()->castTo<AnyFunctionType>();
1786+
auto ty2 = conv->getSubExpr()->getType()->castTo<AnyFunctionType>();
1787+
assert(ty1->withExtInfo(ty1->getExtInfo().withSendable(false))
1788+
->isEqual(ty2->withExtInfo(ty2->getExtInfo().withSendable(false))));
1789+
}
1790+
#endif
1791+
semanticExpr = conv->getSubExpr()->getSemanticsProvidingExpr();
1792+
}
1793+
17811794
if (auto declRef = dyn_cast<DeclRefExpr>(semanticExpr)) {
17821795
setLocFromConcreteDeclRef(declRef->getDeclRef());
17831796
} else if (auto memberRef = dyn_cast<MemberRefExpr>(semanticExpr)) {

lib/Sema/CSApply.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6418,6 +6418,17 @@ maybeDiagnoseUnsupportedFunctionConversion(ConstraintSystem &cs, Expr *expr,
64186418
}
64196419
};
64206420

6421+
// Look through a function conversion that only adds or removes
6422+
// `@Sendable`.
6423+
if (auto conv = dyn_cast<FunctionConversionExpr>(semanticExpr)) {
6424+
auto ty1 = conv->getType()->castTo<AnyFunctionType>();
6425+
auto ty2 = conv->getSubExpr()->getType()->castTo<AnyFunctionType>();
6426+
if (ty1->withExtInfo(ty1->getExtInfo().withSendable(false))
6427+
->isEqual(ty2->withExtInfo(ty2->getExtInfo().withSendable(false)))){
6428+
semanticExpr = conv->getSubExpr()->getSemanticsProvidingExpr();
6429+
}
6430+
}
6431+
64216432
if (auto declRef = dyn_cast<DeclRefExpr>(semanticExpr)) {
64226433
if (auto fn = dyn_cast<FuncDecl>(declRef->getDecl())) {
64236434
return maybeDiagnoseFunctionRef(fn);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-emit-silgen -swift-version 5 -verify %s
2+
3+
// In Swift 6 mode, we introduce an implicit `@Sendable` conversion on the
4+
// function value. Ensure that this doesn't impede C function pointer
5+
// conversion.
6+
// RUN: %target-swift-emit-silgen -swift-version 6 -verify %s
7+
8+
public typealias PDCallbackFunction = @convention(c) (UnsafeMutableRawPointer?) -> Int32
9+
10+
public enum System {
11+
public static func setUpdateCallback(update: @escaping PDCallbackFunction, userdata: UnsafeMutableRawPointer?) {
12+
}
13+
}
14+
15+
@_cdecl("eventHandler")
16+
public func eventHandler(
17+
pointer: UnsafeMutableRawPointer!
18+
) -> Int32 {
19+
System.setUpdateCallback(update: update(pointer:), userdata: nil)
20+
return 0
21+
}
22+
23+
func update(pointer: UnsafeMutableRawPointer!) -> Int32 { 0 }

0 commit comments

Comments
 (0)