Skip to content

[6.0] Look through @Sendable conversions when considering C function pointer conversions. #73437

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
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: 13 additions & 0 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,19 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
loc = declRef.getDecl();
};

if (auto conv = dyn_cast<FunctionConversionExpr>(semanticExpr)) {
// There might be an intermediate conversion adding or removing @Sendable.
#ifndef NDEBUG
{
auto ty1 = conv->getType()->castTo<AnyFunctionType>();
auto ty2 = conv->getSubExpr()->getType()->castTo<AnyFunctionType>();
assert(ty1->withExtInfo(ty1->getExtInfo().withSendable(false))
->isEqual(ty2->withExtInfo(ty2->getExtInfo().withSendable(false))));
}
#endif
semanticExpr = conv->getSubExpr()->getSemanticsProvidingExpr();
}

if (auto declRef = dyn_cast<DeclRefExpr>(semanticExpr)) {
setLocFromConcreteDeclRef(declRef->getDeclRef());
} else if (auto memberRef = dyn_cast<MemberRefExpr>(semanticExpr)) {
Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6418,6 +6418,17 @@ maybeDiagnoseUnsupportedFunctionConversion(ConstraintSystem &cs, Expr *expr,
}
};

// Look through a function conversion that only adds or removes
// `@Sendable`.
if (auto conv = dyn_cast<FunctionConversionExpr>(semanticExpr)) {
auto ty1 = conv->getType()->castTo<AnyFunctionType>();
auto ty2 = conv->getSubExpr()->getType()->castTo<AnyFunctionType>();
if (ty1->withExtInfo(ty1->getExtInfo().withSendable(false))
->isEqual(ty2->withExtInfo(ty2->getExtInfo().withSendable(false)))){
semanticExpr = conv->getSubExpr()->getSemanticsProvidingExpr();
}
}

if (auto declRef = dyn_cast<DeclRefExpr>(semanticExpr)) {
if (auto fn = dyn_cast<FuncDecl>(declRef->getDecl())) {
return maybeDiagnoseFunctionRef(fn);
Expand Down
23 changes: 23 additions & 0 deletions test/SILGen/sendable_c_function_pointer_conversion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-emit-silgen -swift-version 5 -verify %s

// In Swift 6 mode, we introduce an implicit `@Sendable` conversion on the
// function value. Ensure that this doesn't impede C function pointer
// conversion.
// RUN: %target-swift-emit-silgen -swift-version 6 -verify %s

public typealias PDCallbackFunction = @convention(c) (UnsafeMutableRawPointer?) -> Int32

public enum System {
public static func setUpdateCallback(update: @escaping PDCallbackFunction, userdata: UnsafeMutableRawPointer?) {
}
}

@_cdecl("eventHandler")
public func eventHandler(
pointer: UnsafeMutableRawPointer!
) -> Int32 {
System.setUpdateCallback(update: update(pointer:), userdata: nil)
return 0
}

func update(pointer: UnsafeMutableRawPointer!) -> Int32 { 0 }