Skip to content

[cxx-interop] Allow initializing std::function from Swift closures #71396

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 2 commits into from
Jun 17, 2024

Conversation

egorzhdan
Copy link
Contributor

This adds a Swift initializer to instantiations of std::function that accepts a Swift closure with @convention(c).

rdar://103979602

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Feb 5, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

expectEqual(222, res)
}

// FIXME: assertion for address-only closure params
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a major limitation that I intend to fix before merging this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be fixed by #73561

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-std-function branch from e11d309 to 28d3e29 Compare February 6, 2024 16:21
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

This fixes a linker error when using `std::function` from Swift.

rdar://103979602
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-std-function branch 2 times, most recently from 8ebee73 to 2a43d8c Compare June 13, 2024 15:12
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-std-function branch from 2a43d8c to 1bc7e54 Compare June 13, 2024 18:35
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-std-function branch from 1bc7e54 to 7645a40 Compare June 13, 2024 19:03
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

This adds a Swift initializer to instantiations of `std::function` that accepts a Swift closure with `@convention(c)`.

rdar://103979602
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-std-function branch from 8fee0af to 3243228 Compare June 14, 2024 18:06
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan marked this pull request as ready for review June 14, 2024 21:16
@egorzhdan egorzhdan requested a review from Xazax-hun June 14, 2024 21:20
@egorzhdan
Copy link
Contributor Author

I'm going to merge this to unblock some other work, but I'm happy to address review comments post-merge.

@egorzhdan egorzhdan merged commit 7c16527 into main Jun 17, 2024
3 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/cxx-std-function branch June 17, 2024 11:31
#if !os(Windows) // FIXME: rdar://103979602
StdFunctionTestSuite.test("FunctionIntToInt init from closure and call") {
let cClosure: @convention(c) (Int32) -> Int32 = { $0 + 1 }
let f = FunctionIntToInt(cClosure)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to have a test case where the closure actually captures some local state? Or is it not yet supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not supported yet. @convention(c) closures (aka the ones that can be bridged to C function pointers) cannot capture context in Swift. In the future we'll need to add support for converting @convention(thick) closures to std::function, but it requires more work.

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