-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Constrain Sequence methods that return AnySequence #12029
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
[stdlib] Constrain Sequence methods that return AnySequence #12029
Conversation
@swift-ci please test source compatibility |
@swift-ci please test |
There were a couple more in there too but applying the same trivial fix didn't work for them so checking compatibility of these first. |
Build failed |
@swift-ci please test linux platform |
Build failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 I found a couple others on Sequence
, and this weird exception for RandomAccessIndices
: https://github.com/airspeedswift/swift/blob/384989ea79c7f390fa8d408360acff35422352ed/stdlib/public/core/Indices.swift.gyb#L123 — any idea why that's emitted without the constraint?
stdlib/public/core/Sequence.swift
Outdated
SubSequence : Sequence, | ||
SubSequence.Element == Element, | ||
SubSequence.SubSequence == SubSequence { | ||
extension Sequence where SubSequence == AnySequence<Element> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few default implementations in other extensions that should be constrainable:
- suffix(_:)
- split(maxSplits:omitting...:whereSeparator:)
- split(separator:maxSplits:omitting...:) (Does it matter that this is a pure extension and not a requirement?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re that last one, I suppose it does, since it means you couldn't just use that method in a Sequence where Element: Equatable
extension/generic. Hmmm. There's no way to implement it just in terms of returning SubSequence
, since it needs to eagerly consume the sequence in order to allow random access to the chunks in the array. Maybe that should be converted to return [[Element]]
instead? Not sure what we're getting out of it returning an AnySequence
wrapping an array if it doesn't need to match a SubSequence
requirement anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that should be converted to return
[[Element]]
instead?
Hmmm. There's possible a good case for the default split on sequences being lazy and non-consuming. Anyway, not such a slam-dunk as the prefix
/suffix
cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to consume the sequence, since it returns an array and someone might write something like:
let result = mySinglePassSequence.split(separator: " ")
let lastChunk = result.last!
We could have a lazy version that returns a non-array sequence, but these ones need to be eager.
@swift-ci Please test Linux platform |
384989e
to
75df81b
Compare
@swift-ci please test |
75df81b
to
f338aa2
Compare
This should be unblocked now |
bf5c960
to
f85b87f
Compare
@swift-ci please test |
@swift-ci please test source compatibility |
@natecook1000 yeah I can't spot why that's necessary either. Possibly working around a since-fixed compiler crasher? |
@swift-ci please smoke benchmark |
@swift-ci please smoke test |
@swift-ci please smoke test compiler performance |
!!! Couldn't read commit file !!! |
Build comment file:Optimized (O)Regression (3)
Improvement (22)
No Changes (304)
Unoptimized (Onone)Regression (3)
Improvement (3)
No Changes (323)
Hardware Overview
|
@swift-ci please smoke test compiler performance |
stdlib/public/core/Indices.swift.gyb
Outdated
@@ -120,9 +120,6 @@ public struct ${Self}< | |||
} | |||
|
|||
extension ${collectionForTraversal(Traversal)} | |||
% if Traversal != 'RandomAccess': | |||
where Indices == ${Self}<Self> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the % if ...
should go away here, right? We want this extension to be constrained to where Self.Indices == DefaultIndices<Self>
for each specialized version of Default____Indices
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d'oh. right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to do this part in a separate PR along with another change.
Hoping this will clear up the failures in the newly-re-enabled AsyncNinja. |
@swift-ci Please test source compatibility |
fd4fec8
to
f85b87f
Compare
Currently
Sequence
's defaultSubSequence
isAnySequence
, but it gets it in a roundabout way. This does it more explicitly, and then constrains the default implementations to sequences that don't have a custom subsequence (including collections, which do this via slicing instead).This means you no longer get a bunch of confusing overloads on collections like
Array
:Built on top of #11923, for which I pine. It might not need it.
rdar://problem/34548873