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-1863] for loop produces compiler error if the base class of the instance conforms to Collection protocol #44472

Open
swift-ci opened this issue Jun 22, 2016 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1863
Radar rdar://problem/26941991
Original Reporter cezheng (JIRA User)
Type Bug
Environment

Xcode Version 8.0 Beta

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @slavapestov
Priority Medium

md5: 7929fc5fb48ff92511ff43aa4c401f7a

Issue Description:

I have a class that conforms to the Collection protocol, with some other classes inheriting from it.

But when I try to use for loop on instances of the derived classes, the Swift compiler complains:

`'IndexingIterator<DerivedCustomCollection>' is not convertible to 'Iterator' (aka 'IndexingIterator<CustomCollection>')`

Then I figured out that specifying the Iterator type alias will resolve the problem, but then another problem emerged: I cannot use for loop on the enumerated() if I do this..

I have put the code to reproduce this bug into a gist:
https://gist.github.com/cezheng/59c90a48fa13782482fe95fe87bb6339

Just copy the code into an Xcode 8 playground and you will immediately see the problem.

Thanks🙂

@belkadan
Copy link
Contributor

@gribozavr, @moiseev, any idea what's going on?

@moiseev
Copy link
Mannequin

moiseev mannequin commented Jun 22, 2016

Protocol conformance for classes is tricky. Previously one could have added another explicit conformance to the derived type, but now, when multiple conformances are a compiler error, this won't work. The workaround is to use structs and composition instead of classes and inheritance.
Also CC @slavapestov, who might be able to shed more light on classes and protocols.

@slavapestov
Copy link
Member

I think a strategically-placed 'as Self' cast inside the IndexingIterator implementation might work. The problem is there's a type parameter that is bound to the dynamic Self type and not the static Self type of the conformance. I'll take a look.

@Dante-Broggi
Copy link
Contributor

Is this resolved, or no longer valid? If either, this should be closed.

@slavapestov
Copy link
Member

No, it's not resolved.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants