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-2819] Commit d9650fb breaks dual conforming RandomAccessCollection, RangeReplaceableCollection collections. #45423
Comments
cc @moiseev |
A simple fix would be to explicitly say what your slice is. There is `RangeReplaceableRandomAccessSlice`. It might not be enough to simply say `typealias SubSequence = RangeReplaceableRandomAccessSlice<..>` though. What usually works is overriding a subscript that will simply construct the said slice instance. As for the proper fix, I think, providing an `extension RandomAccessCollection where Self == RangeReplaceableCollection { ... }` with the same kind of subscript implementation as described above, should solve it. mattgallagher (JIRA User), do you mind trying it and submitting a PR maybe? It would also be great to include a test case for this particular issue. |
I actually had trouble using the explicit typealias in a personal project the other day, but haven't had the chance to file the issue. The compiler detects a circularity in passing the Self type as the generic parameter to the slice, and rejects the conformance. I had to trick it by writing the slicing subscript and having it infer the type. |
@belkadan We saw multiple instances of this problem while implementing the new indexing model, so I believe there is already a radar for that. And yes, you're right, implementing a subscript usually helps. |
Comment by Matt Gallagher (JIRA) @moiseev as @belkadan mentions, attempting to explicitly set the typealias in the type as follows: typealias SubSequence = RangeReplaceableRandomAccessSlice<Self> gives the error "Type alias `SubSequence` circularly references itself". I didn't think to try implementing the `subscript(:Range<Index>)` operator. That does offer a useable workaround for the problem when the `subscript` is defined on the type itself. However, your suggested fix in the standard library – defining the `subscript` in an extension – does not appear work. The following extension: extension RandomAccessCollection where Self: RangeReplaceableCollection {
public subscript(bounds: Range<Self.Index>) -> RangeReplaceableRandomAccessSlice<Self> {
return RangeReplaceableRandomAccessSlice(base: self, bounds: bounds)
}
} merely appears to create further ambiguity (causing errors all over the standard library). I also tried adding `SubSequence == RangeReplaceableRandomAccessSlice<Self>` to the extension constraints, to no avail. |
Well.. It was worth trying at least. Thanks for sharing your experience. Does it mean that your immediate problem was solved by introducing the subscript? Or do you need another solution? |
Comment by Matt Gallagher (JIRA) @moiseev My specific use case is solved with the subscript, yes. I'll leave it to you guys to decide if you want to take further steps or close this issue as "work around available" for now. Frankly, I think the biggest problem is the limitation imposed by the "Type alias `SubSequence` circularly references itself" but I'm sure you have bugs to fix that already. |
The "Type alias `SubSequence` circularly references itself" thing is fixed on master. |
Additional Detail from JIRA
md5: fc76bbdc66732e5a4a9aca424ccb88e0
Issue Description:
The following code fails to compile in the 2016-10-01 snapshot, despite working in previous Swift 3 builds:
Remove either of the `RandomAccessCollection` or `RangeReplaceableCollection` conformances and it will compile. Build in an earlier version of Swift (e.g. Xcode 8 or Xcode 8.1 beta 1) and there's no problem.
The issue appears to be ambiguity problems around `SubSequence` that cannot be easily resolved.
The commit responsible is d9650fb:
d9650fb
Reverting this commit eliminates the problem. I'm stopping short of a pull request that reverts this commit but I think it should be considered.
It's possible that there's a smarter way to resolve the ambiguity that I'm not seeing. Perhaps it's considered nonsensical to implement both these protocols? Perhaps simply better fix suggestions are required to guide towards a solution?
The text was updated successfully, but these errors were encountered: