Skip to content

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

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
merged 4 commits into from
Jun 18, 2024

Conversation

ahatanaka
Copy link
Contributor

@ahatanaka ahatanaka commented May 10, 2024

When emitting a native-to-foreign thunk, pass the thunk's result address parameter to the native function if both the thunk and the native function return their results indirectly and the thunk is not for an async function.

Also, remove an outdated assertion.

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 ahatanaka requested review from rjmccall and DougGregor 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
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried compiling the following code, where S is a non-trivial C++ type, but the compiler rejects it:

@objc
class C0 : NSObject {
  // error: method cannot be marked @objc because its result type cannot be represented in Objective-C
  // note: non-trivial C++ classes cannot be represented in Objective-C
  @objc
  func m1() -> S {
    return S()
  }
}

The diagnostic was added in #69343.

Is there another way to trigger the assertion?

Copy link
Contributor

@rjmccall rjmccall Jun 13, 2024

Choose a reason for hiding this comment

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

That seems quite wrong. I mean, we can have this restriction if we need to subset things, but it is absolutely not the case that non-trivial C++ classes cannot be represented in Objective-C. We can import methods like this from Objective-C++ headers even if we restrict them in Swift for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

At any rate, please write the code assuming that this is required to work, even if there's currently a restriction about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I removed the assertion altogether.

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

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
Contributor

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.

@ahatanaka ahatanaka force-pushed the closure-to-fp-indirect-return branch from 01add11 to 9ee583e Compare June 6, 2024 03:46
@ahatanaka
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

Alright, LGTM

@ahatanaka ahatanaka merged commit 1b0df4e into main Jun 18, 2024
5 checks passed
@ahatanaka ahatanaka deleted the closure-to-fp-indirect-return branch June 18, 2024 22:03
meg-gupta pushed a commit to meg-gupta/swift that referenced this pull request Jun 20, 2024
…nction returning a non-trivial C++ type (swiftlang#73561)

When emitting a native-to-foreign thunk, pass the thunk's result address parameter to the native function if both the thunk and the native function return their results indirectly and the thunk is not for an async function.

Also, remove an outdated assertion.

rdar://124501345
ahatanaka added a commit that referenced this pull request Jun 20, 2024
…nction returning a non-trivial C++ type (#73561)

When emitting a native-to-foreign thunk, pass the thunk's result address parameter to the native function if both the thunk and the native function return their results indirectly and the thunk is not for an async function.

Also, remove an outdated assertion.

rdar://124501345
(cherry picked from commit 1b0df4e)
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.

2 participants