Skip to content
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

[SR-7911] FlattenSequence.Iterator no longer conforms to Sequence #50446

Closed
an0 mannequin opened this issue Jun 7, 2018 · 8 comments
Closed

[SR-7911] FlattenSequence.Iterator no longer conforms to Sequence #50446

an0 mannequin opened this issue Jun 7, 2018 · 8 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression standard library Area: Standard library umbrella swift 4.1 type checker Area → compiler: Semantic analysis

Comments

@an0
Copy link
Mannequin

an0 mannequin commented Jun 7, 2018

Previous ID SR-7911
Radar rdar://problem/42312634
Original Reporter @an0
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 10 beta 1

Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug, 4.1Regression, TypeChecker
Assignee @airspeedswift
Priority Medium

md5: 0b5674778a68e9f7a21ddc511b8b30ad

Issue Description:

It seems Swift 4.2 compiler doesn't think FlattenSequence<LazyMapCollection<[Foo], [Bar]>>.Iterator is a Sequence of Bar. It's a regression, it worked in Swift 4.1.

class Foo {
    var bars = [Bar]()
}

class Bar {
    
}

var bars = [Bar]()
var foos = [Foo]()
var barIterator = foos.lazy.flatMap { $0.bars }.makeIterator()
bars.append(contentsOf: barIterator) // error: extraneous argument label 'contentsOf:' in call
@belkadan
Copy link
Contributor

belkadan commented Jun 8, 2018

@lorentey, are Iterators Sequences? Is there a special lazy flatMap?

@rudkx, do you know of anything that changed here?

@rudkx
Copy link
Member

rudkx commented Jun 8, 2018

In 4.1, barIterator is inferred to be a FlattenIterator<LazyMapIterator<IndexingIterator<Array<Foo>>, [Bar]>>, whereas in 4.2 it is inferred to be a FlattenSequence<LazyMapCollection<[Foo], [Bar]>>.Iterator.

Offhand I don't know if that's a result of a standard library change or a something that happens differently during expression type checking.

@slavapestov
Copy link
Member

Isn't `FlattenIterator<LazyMapIterator<IndexingIterator<Array<Foo>>, [Bar]>>` the desugared form of `FlattenSequence<LazyMapCollection<[Foo], [Bar]>>.Iterator`? In 4.2 we made type alias types preserve sugar in more cases. However this should not have changed behavior.

@slavapestov
Copy link
Member

Actually I'm wrong, `FlattenSequence.Iterator` is now a bona-fide struct.

@slavapestov
Copy link
Member

Ok I see. In 4.1, FlattenIterator conformed to Sequence too. We have a default implementation of Sequence for Self : IteratorProtocol where makeIterator() just returns self.

Adding this conformance to the original code example makes it compile:

extension FlattenSequence.Iterator : Sequence {}

However I think it makes more sense to change the last two lines to:

var barSequence = foos.lazy.flatMap { $0.bars }
bars.append(contentsOf: barSequence)

That is, just dropping the makeIterator() call.

@airspeedswift do you want to add the "FlattenSequence.Iterator : Sequence" conformance, or just close this as working as expected?

@an0
Copy link
Mannequin Author

an0 mannequin commented Jun 9, 2018

@slavapestov Thanks for the 1st workaround. I don't use the 2nd because I iterate barIterator using next() and stops when some condition is met and append the rest to bars. I can, by manually keeping track of indices(I'm actually iterating 2 such iterators, I'm doing merging), but I think the iterator version is more elegant. @airspeedswift I think Sequence.Iterator should be a sequence.

@an0
Copy link
Mannequin Author

an0 mannequin commented Jun 10, 2018

@slavapestov I have issues with the 1st workaround: since #if swift(>=4.2) tests language version instead of compiler version, how can I make my project compilable in both Xcode 9.4 and Xcode 10?

@airspeedswift
Copy link
Member

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression standard library Area: Standard library umbrella swift 4.1 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants