Skip to content

[cxx-interop] Add CxxSequence protocol to the stdlib overlay #59492

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 1 commit into from
Jun 28, 2022

Conversation

egorzhdan
Copy link
Contributor

This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection Swifty by adding conformances to Swift.Sequence, Swift.Collection, etc.

This is not meant to be a final design.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jun 16, 2022
/// Returns the unwrapped result of C++ `operator*()`.
///
/// Generally, Swift creates this property automatically for C++ types that define `operator*()`.
var pointee: Pointee { get }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Name chosen to match UnsafePointer.pointee/UnsafeMutablePointer.pointee and the synthesized properties for operator*().

var pointee: Pointee { get }

/// Returns an iterator pointing to the next item in the sequence.
func successor() -> Self
Copy link
Contributor Author

@egorzhdan egorzhdan Jun 16, 2022

Choose a reason for hiding this comment

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

Name chosen to match UnsafePointer.successor()/UnsafeMutablePointer.successor(): I think we should try to avoid adding public methods/properties to Swift stdlib classes like UnsafePointer as much as possible, and reusing the existing name allows us to define an empty extension with no public methods.

@egorzhdan egorzhdan requested review from zoecarver, hyp and Huddie June 16, 2022 18:14
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

// REQUIRES: OS=macosx || OS=linux-gnu

import CustomSequence
import std
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test for std.string and/or std.vector? (Maybe in another file.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Or better yet: update the existing string/vector tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap, added a test for std::string. std::vector is more tricky because it's a template type.


for item in seq {
print(item)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Simply executing the code without validating the results doesn't seem like an adequate test.

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 typechecker test, we're not actually executing any code here. This test makes sure that the typechecker doesn't emit any unexpected warnings or remarks.

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-overlay-sequence branch from 58ae64c to dd195cd Compare June 17, 2022 12:39
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test


extension UnsafePointer: UnsafeCxxInputIterator {}

extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you conform Optional<UnsafePointer> to UnsafeCxxInputIterator? That way you can allow nullable pointers returned from begin/end, which some empty containers can return.

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 a great idea, thanks!
That currently means that we have to add new public members to Optional<UnsafePointer> (pointee & successor()), which I think would be best to avoid, since users might start relying on them in non-interop code, but I'll think about how to hide those members a bit later. We can probably fix that by introducing a separate private iterator protocol.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to confirm: if c++ interop is not enabled, people won't get the overlay, therefore won't see these members/conformances?

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 is correct, these conformances will only be visible when C++ interop is enabled.

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-overlay-sequence branch 10 times, most recently from 50cb704 to 4203720 Compare June 27, 2022 14:18
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection safe and Swifty by adding conformances to `Swift.Sequence`, `Swift.Collection`, etc.

This is not meant to be a final design.
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-overlay-sequence branch from 4203720 to 6754c3c Compare June 27, 2022 19:40
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit c49f174 into main Jun 28, 2022
@egorzhdan egorzhdan deleted the egorzhdan/cxx-overlay-sequence branch June 28, 2022 09:58
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.

4 participants