Swift 2: Regression: Protocol typealiases & enum cases share a namespace
| Originator: | rix.rob | ||
| Number: | rdar://21293473 | Date Originated: | 08-Jun-2015 08:43 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-7-beta (7A120f) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
In Swift 1.2, an enum could conform to a protocol with typealiases shadowing the names of its cases. This worked out just fine in practice because they could never actually collide; the types were available in constraints and the cases were available when constructing instances of the type.
Swift 2 has regressed this, erroring out with unhelpful (bogus) paths.
Steps to Reproduce:
1. Compile this code:
protocol EitherType {
typealias Left
typealias Right
func either<V>(ifLeft ifLeft: Left -> V, ifRight: Right -> V) -> V
}
enum Either<T, U>: EitherType {
case Left(T)
case Right(U)
func either<V>(ifLeft ifLeft: T -> V, ifRight: U -> V) -> V {
switch self {
case let .Left(left):
return ifLeft(left)
case let .Right(right):
return ifRight(right)
}
}
}
Expected Results:
Success.
Actual Results:
Bad errors:
either.Either:5:13: error: invalid redeclaration of 'Left'
typealias Left = T
^
./either.swift:11:7: note: 'Left' previously declared here
case Left(T)
^
either.Either:6:13: error: invalid redeclaration of 'Right'
typealias Right = U
^
./either.swift:12:7: note: 'Right' previously declared here
case Right(U)
^
NB: the errors themselves state that the redeclaration is in either.Either, which isn’t actually a Swift source file. Xcode therefore can’t show me the source which the errors reference, which is the icing on the cake.
Regression:
This compiled fine in Swift 1.2 (modulo the intrusion of Box or equivalent).
Notes:
N/A
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!