Swift: Constrained associated types can’t be same-type constrained to parameters with the same constraints
| Originator: | rix.rob | ||
| Number: | rdar://19245317 | Date Originated: | 14-Dec-2014 03:03 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.1.1 (6A2008a) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
When an associated type is constrained to a protocol and the type parameters of a method or constructor attempt to constrain the associated type to be equal to a type parameter constraining to the same protocol, the compiler gives spurious errors.
Steps to Reproduce:
1. Compile this:
protocol P {
typealias T: P
}
struct S<A: P> {
init<Q: P where Q.T == A>(_ q: Q) {}
}
Expected Results:
I expected it to compile. Q.T is constrained to conform to P by the protocol’s definition; A is constrained to conform to P by the struct’s definition; what’s the problem?
Actual Results:
Spurious errors:
./same-type.swift:9:22: error: same-type constraint type 'A' does not conform to required protocol 'P'
Regression:
These errors do not occur if P.T does not itself have any constraints.
You can work around this by constructing S with a free function, which is unidiomatic, but actually compiles.
A static method on S works in this case, but not in others, e.g. a CollectionOf type. (Still investigating that.)
Notes:
This bites me surprisingly frequently—any time I’m trying to do something interesting with CollectionType, for example.
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!