<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">Consider this code,</div><div class=""><br class=""></div><div class="">class Base : Collection {<br class="">&nbsp;&nbsp;var startIndex: Int { return 0 }<br class=""><br class="">&nbsp;&nbsp;var endIndex: Int { return 10 }<br class=""><br class="">&nbsp;&nbsp;func index(after i: Int) -&gt; Int { return i + 1 }<br class=""><br class="">&nbsp;&nbsp;subscript(index: Int) -&gt; Int { return index }<br class="">}<br class=""><div class=""><br class=""></div><div class="">We infer the associated type ‘Iterator’ as ‘IndexingIterator&lt;Base&gt;’. I can use an instance of Base as a sequence just fine:</div><div class=""><br class=""></div>for x in Base() {} // OK</div><br class="">Now if I subclass Base, the associated type is still ‘IndexingIterator&lt;Base&gt;’:<div class=""><br class="">class Derived : Base {}<br class=""><br class="">However the implementation of makeIterator is defined in a constrained extension by the standard library,</div><div class=""><br class=""></div><div class="">extension Collection where Self.Iterator == IndexingIterator&lt;Self&gt; {</div><div class="">&nbsp; func makeIterator() -&gt; IndexingIterator&lt;Self&gt; { … }</div><div class="">}<br class=""><br class=""></div><div class="">So I cannot call it on a subclass:</div><div class=""><br class=""></div><div class="">for x in Derived() {} // fails</div><div class=""><br class=""></div><div class="">The error is bizarre, "'IndexingIterator&lt;Base&gt;' is not convertible to 'IndexingIterator&lt;Derived&gt;’” — I’m not doing a conversion here.</div><div class=""><br class=""></div><div class="">If you try to call makeIterator() directly, you get an ambiguity error instead:</div><div class=""><br class=""></div><div class="">col.swift:17:5:&nbsp;error:&nbsp;ambiguous reference to member 'makeIterator()'<br class="">_ = Derived().makeIterator()<br class="">&nbsp; &nbsp;&nbsp;^~~~~~~~~<br class="">Swift.Collection:6:17:&nbsp;note:&nbsp;found this candidate<br class="">&nbsp; &nbsp;&nbsp;public func makeIterator() -&gt; IndexingIterator&lt;Self&gt;<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;^<br class="">Swift.Sequence:5:17:&nbsp;note:&nbsp;found this candidate<br class="">&nbsp; &nbsp;&nbsp;public func makeIterator() -&gt; Self<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;^<br class=""><br class=""></div><div class="">Now I couldn’t come up with an example where the code compiles but crashes at runtime because of a type mismatch, but it’s not outside the realm of possibility.</div><div class=""><br class=""></div><div class="">With my PR here the conformance itself no longer type checks:&nbsp;<a href="https://github.com/apple/swift/pull/12174" class="">https://github.com/apple/swift/pull/12174</a></div><div class=""><br class=""></div><div class="">col.swift:1:7:&nbsp;error:&nbsp;type 'Base' does not conform to protocol 'Collection'<br class="">class Base : Collection {<br class="">&nbsp; &nbsp; &nbsp;&nbsp;^<br class="">Swift.Sequence:5:17:&nbsp;note:&nbsp;candidate has non-matching type '&lt;Self&gt; () -&gt; Self' [with Element = Int, Index = Int, IndexDistance = Int, Iterator = IndexingIterator&lt;Base&gt;, SubSequence =&nbsp;Slice&lt;Base&gt;, Indices = DefaultIndices&lt;Base&gt;]<br class="">&nbsp; &nbsp;&nbsp;public func makeIterator() -&gt; Self<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;^<br class="">Swift.Collection:6:17:&nbsp;note:&nbsp;candidate has non-matching type '&lt;Self&gt; () -&gt; IndexingIterator&lt;Self&gt;' [with Element = Int, Index = Int, IndexDistance = Int, Iterator = IndexingIterator&lt;Base&gt;,&nbsp;SubSequence = Slice&lt;Base&gt;, Indices = DefaultIndices&lt;Base&gt;]<br class="">&nbsp; &nbsp;&nbsp;public func makeIterator() -&gt; IndexingIterator&lt;Self&gt;</div><div class=""><br class=""></div><div class="">I found one example in our code base where this pattern comes up, and that’s SyntaxCollection in&nbsp;tools/SwiftSyntax/SyntaxCollection.swift. It has no subclasses so making it final works there.</div><div class=""><br class=""></div><div class="">This was reported externally as&nbsp;<a href="https://bugs.swift.org/browse/SR-1863" class="">https://bugs.swift.org/browse/SR-1863</a>. I’m not sure if the user expects it to work or just to produce a reasonable diagnostic instructing them to make the class final.</div><div class=""><br class=""></div><div class="">What does everyone think of this?</div><div class=""><br class=""></div><div class="">1) Can anyone suggest a way to make it work, so that ‘for x in Derived()’ type checks and the correct Self type (Base, not Derived) for the substitution?</div><div class=""><br class=""></div><div class="">2) Should we just ban such ’non-covariant’ conformances? There is precedent for this — in Swift 3, we used to allow non-final classes to conform to protocols whose requirements had same-type constraints with the right hand side equal to ‘Self’, and Doug closed this hole in Swift 4. My PR is essentially a more comprehensive fix for this hole.</div><div class=""><br class=""></div><div class="">3) Should we allow the hole to remain in place, admitting non-final classes that model Collection, at the cost of not being able to ever fix&nbsp;<a href="https://bugs.swift.org/browse/SR-617" class="">https://bugs.swift.org/browse/SR-617</a>?</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""></div></body></html>