Skip to content
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

[SILGen] Fix a crash when a closure is converted to a pointer to a function returning a non-trivial C++ type #73561

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ahatanaka
Copy link
Contributor

@ahatanaka ahatanaka commented May 10, 2024

Pass the thunk's result address parameter to the closure when the thunk is for a C function.

Also, fix the predicate function passed to emitEntryPointIndirectReturn. The predicate should return true only when the result is returned directly at the SIL level but indirectly at the IR level.

rdar://124501345

function returning a non-trivial C++ type

rdar://124501345
@ahatanaka
Copy link
Contributor Author

@swift-ci please test

@ahatanaka ahatanaka added the c++ interop Feature: Interoperability with C++ label May 10, 2024
@ahatanaka ahatanaka marked this pull request as ready for review May 10, 2024 17:06
@ahatanaka
Copy link
Contributor Author

@swift-ci please test

@ahatanaka
Copy link
Contributor Author

@swift-ci please test macOS

assert((SGF.F.getRepresentation() ==
SILFunctionType::Representation::CFunctionPointer ||
objcFnTy->getNumIndirectFormalResults() == 0) &&
"Objective-C methods cannot have indirect results");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is just wrong now that we're importing types that are formally treated as indirect in SIL. The test case here is just an ObjC++ method that returns a non-trivial C++ type, or even an ObjC method that returns a struct that contains a __weak reference (although we may not support that properly). We should just remove this assertion. Please check whether that test case passes without any other changes.

for (auto result : substConv.getResults()) {
if (!substConv.isSILIndirect(result)) {
continue;
if (F.getRepresentation() ==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition should not be looking at function type representations and should instead be looking at, I guess, whether there's a direct/indirect mismatch in the result.

if (substConv.hasIndirectSILResults()) {
assert(substTy->getNumResults() == 1);
result = args[0];
if (F.getRepresentation() != SILFunctionType::Representation::CFunctionPointer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here as above. This should be based intelligently on looking at the differences between the result types and whether they're returned directly, not the function type representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants