Skip to content

[PrintAsClang] Fix thunks for Never funcs #72402

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 3 commits into from
Mar 30, 2024

Conversation

beccadax
Copy link
Contributor

@beccadax beccadax commented Mar 18, 2024

Swift-to-C++ thunk printing for functions didn’t really take into account Swift’s Never type. This type maps to a void return with the SWIFT_NORETURN attribute, but it also requires other tweaks to code generation, such as omitting the return keyword. (Removing that requires minor changes to many tests.)

This PR also contains a follow-up fix for throwing Never functions, which have a number of subtleties.

Fixes rdar://124137073.

@beccadax beccadax added the swift to c++ Feature → c++ interop: swift to c++ label Mar 18, 2024
@beccadax
Copy link
Contributor Author

@swift-ci please test

@beccadax
Copy link
Contributor Author

@hyp If you're still available, it sounds like you're the best person to review changes to this part of the compiler.

if (isResultType && Count == 0) {
// A direct result with no record members can happen for uninhabited result
// types like `Never`.
os << "void";
Copy link
Contributor Author

@beccadax beccadax Mar 18, 2024

Choose a reason for hiding this comment

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

When I was working on this PR, I noticed that IRGen represents Never return types as having one empty-tuple direct result, rather than as having no direct result. (This previously tripped an assert.) I'm assuming that this is correct, or at least that changing it would break the ABI, and that a void return with swiftcall is the correct prototype to use to call such a function from C++.

I'd love to get confirmation from someone who works on IRGen (maybe @aschwaighofer or @rjmccall?) that this is the right approach.

@beccadax beccadax requested a review from ahatanaka March 19, 2024 19:33
Copy link
Contributor

@hyp hyp left a comment

Choose a reason for hiding this comment

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

Thanks!

@beccadax
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@beccadax
Copy link
Contributor Author

@swift-ci please test

Swift-to-C++ thunk printing for functions didn’t really take into account Swift’s `Never` type. This type maps to `SWIFT_NORETURN`, but it also requires other tweaks to code generation, such as omitting the `return` keyword. (Removing that requires minor changes to many tests.)

Fixes rdar://124137073.
Extend the previous commit’s support for functions that return Never to also properly generate code for *throwing* Never functions. This is a little subtle because:

• At the SWIFT_CALL level, throwing Never functions are *not* noreturn
• At the thunk level, throwing Never functions are noreturn *only* if you’re using exceptions; if you’re using swift::Expected, they should throw
• In either case, the compiler cannot statically prove that thunks are noreturn except on the error path, so we need to add an abort() call on the success path
`simctl spawn` does not propagate exit codes through, so we need the test to exit using `exit(0)`, not `fatalError()`.
@beccadax beccadax force-pushed the noreturn-for-tomorrow branch from 3ec1190 to fa2ebd9 Compare March 30, 2024 07:32
@beccadax
Copy link
Contributor Author

@swift-ci please test

@beccadax beccadax enabled auto-merge March 30, 2024 07:34
@beccadax beccadax merged commit ee24bdf into swiftlang:main Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift to c++ Feature → c++ interop: swift to c++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants