Conforming String.CharacterView.Index to Strideable: fatal error when using stride(to:by:): "cannot increment endIndex "
<h2>Question:</h2>

<p>When attempting to stride over <code>String.CharacterView.Index</code> indices by e.g. a stride of <code>2</code> </p>

<pre><code>extension String.CharacterView.Index : Strideable { }

let str = "01234"
for _ in str.startIndex.stride(to: str.endIndex, by: 2) { } // fatal error
</code></pre>

<p>I get the following runtime exception </p>

<blockquote>
  <p>fatal error: cannot increment endIndex </p>
</blockquote>

<p>Just creating the <code>StrideTo&lt;String.CharacterView.Index&gt;</code> above, however, (<code>let foo = str.startIndex.stride(to: str.endIndex, by: 2)</code>) does not yield an error, only when attempting to stride/iterate over or operate on it (<code>.next()</code>?).</p>

<ul>
<li>What is the reason for this runtime exception; is it expected (mis-use of conformance to <code>Stridable</code>)? </li>
</ul>

<p>I'm using <strong>Swift 2.2</strong> and <strong>Xcode 7.3</strong>. Details follow below.</p>

<p><strong>Edit addition: error source located</strong></p>

<p>Upon reading my question carefully, it would seem as if the error really does occur in the <code>next()</code> method of <code>StrideToGenerator</code> (see bottom of this post), specifically at the following marked line</p>

<pre><code>let ret = current
current += stride  // &lt;-- here
return ret
</code></pre>

<p>Even if the last update of <code>current</code> will never be returned (in next call to <code>next()</code>), the final advance of <code>current</code> index to a value larger or equal to that of <code>_end</code> yields the specific runtime error above (for <code>Index</code> type <code>String.CharacterView.Index</code>).</p>

<pre><code>(0..&lt;4).startIndex.advancedBy(4) // OK, -&gt; 4
"foo".startIndex.advancedBy(4)   // fatal error: cannot increment endIndex
</code></pre>

<p>However, one question still remains:</p>

<ul>
<li>Is this a bug in the <code>next()</code> method of <code>StrideToGenerator</code>, or just an error that pops up due to a mis-use of <code>String.CharacterView.Index</code> conformance to <code>Stridable</code>?</li>
</ul>

<hr>

<h2>Related</h2>

<p>The following Q&amp;A is related to <em>the subject of a iterating over characters in steps other than <code>+1</code></em>, and worth including in this question even if the two questions differ.  </p>

<ul>
<li><a href="https://stackoverflow.com/questions/36194060/using-string-characterview-index-successor-in-for-statements/">Using String.CharacterView.Index.successor() in for statements</a></li>
</ul>

<p>Especially note <a href="https://stackoverflow.com/a/36207901/4573247">@Sulthan:s neat solution</a> in the thread above.</p>

<hr>

<h2>Details</h2>

<p><em>(Apologies for hefty details/investigations of my own, just skip these sections if you can answer my question without the details herein)</em></p>

<p>The <a href="http://swiftdoc.org/v2.2/type/String.CharacterView.Index/" rel="nofollow noreferrer"><code>String.CharacterView.Index</code> type</a> describes a character position, and:</p>

<ul>
<li>conforms to <code>Comparable</code> (and in so, <code>Equatable</code>), </li>
<li>contains implementations for <code>advancedBy(_:)</code> and <code>distanceTo(_:)</code>.</li>
</ul>

<p>Hence, it can directly be made to conform to the <a href="http://swiftdoc.org/v2.2/protocol/Strideable/" rel="nofollow noreferrer">protocol <code>Strideable</code></a>, making use of <code>Stridable</code>:s default implementations of methods <code>stride(through:by:)</code> and <code>stride(to:by:)</code>. The examples below will focus on the latter (analogous problems with the former):</p>

<blockquote>
  <p>...</p>

<pre><code>func stride(to end: Self, by stride: Self.Stride) -&gt; StrideTo&lt;Self&gt;
</code></pre>
  
  <p>Returns the sequence of values <em>(self, self + stride, self + stride +
  stride, ... last)</em> where last is the last value in the progression
  <strong>that is less than end</strong>.</p>
</blockquote>

<p><strong>Conforming to <code>Stridable</code> and striding by <code>1</code>: all good</strong></p>

<p>Extending <code>String.CharacterView.Index</code> to <code>Stridable</code> and striding by <code>1</code> works fine:</p>

<pre><code>extension String.CharacterView.Index : Strideable { }

var str = "0123"

// stride by 1: all good
str.startIndex.stride(to: str.endIndex, by: 1).forEach {
    print($0,str.characters[$0])
} /* 0 0 
     1 1 
     2 2
     3 3 */
</code></pre>

<p>For an even number of indices in <code>str</code> above (indices <code>0..&lt;4</code>), this also works for a stride of <code>2</code>:</p>

<pre><code>// stride by 2: OK for even number of characters in str.
str.startIndex.stride(to: str.endIndex, by: 2).forEach {
    print($0,str.characters[$0])
} /* 0 0
     2 2 */
</code></pre>

<p><strong>However, for some cases of striding by <code>&gt;1</code>: runtime exception</strong></p>

<p>For an odd number of indices and a stride of <code>2</code>, however, the stride over the character views indices yield a runtime error</p>

<pre><code>// stride by 2: fatal error for odd number of characters in str.
str = "01234"
str.startIndex.stride(to: str.endIndex, by: 2).forEach {
    print($0,str.characters[$0])
} /* 0 0
     2 2
     fatal error: cannot increment endIndex */
</code></pre>

<h2>Investigations of my own</h2>

<p>My own investigations into this made me suspect the error comes from the <code>next()</code> method of the <a href="http://swiftdoc.org/v2.2/type/StrideToGenerator/" rel="nofollow noreferrer"><code>StrideToGenerator</code> structure</a>, possibly when this method calls <code>+=</code> on the stridable element</p>

<pre><code>public func += &lt;T : Strideable&gt;(inout lhs: T, rhs: T.Stride) {
  lhs = lhs.advancedBy(rhs)
}
</code></pre>

<p>(from a version of the Swift source for <a href="https://github.com/apple/swift/blob/174d475833838fda986120dabc009377ffaa89b6/stdlib/public/core/Stride.swift" rel="nofollow noreferrer"><code>swift/stdlib/public/core/Stride.swift</code></a> that somewhat corresponds to Swift 2.2). Given the following Q&amp;A:s</p>

<ul>
<li><a href="https://stackoverflow.com/questions/29277746/trim-end-off-of-string-in-swift-getting-error-at-runtime">Trim end off of string in swift, getting error at runtime</a>,</li>
<li><a href="https://stackoverflow.com/questions/27191865/swift-distance-method-throws-fatal-error-can-not-increment-endindex">Swift distance() method throws fatal error: can not increment endIndex</a>,</li>
</ul>

<p>we could suspect that we would possibly need to use <code>String.CharacterView.Index.advancedBy(_:limit:)</code> rather than <code>...advancedBy(_:)</code> above. <strike>However from what I can see, the <code>next()</code> method in <code>StrideToGenerator</code> guards against advancing the index past the limit.</strike> </p>

<p><strong>Edit addition:</strong> the source of the error seems to indeed be located in the <code>next()</code> method in <code>StrideToGenerator</code>:</p>

<pre><code>// ... in StrideToGenerator
public mutating func next() -&gt; Element? {
    if stride &gt; 0 ? current &gt;= end : current &lt;= end {
        return nil
    }
    let ret = current
    current += stride /* &lt;-- will increase current to larger or equal to end 
                               if stride is large enough (even if this last current
                               will never be returned in next call to next()) */
    return ret
}
</code></pre>

<p>Even if the last update of <code>current</code> will never be returned (in next call to <code>next()</code>), the final advance of <code>current</code> index to a value larger or equal to that of <code>end</code> yields the specific runtime error above, for <code>Index</code> type <code>String.CharacterView.Index</code>. </p>

<pre><code>(0..&lt;4).startIndex.advancedBy(4) // OK, -&gt; 4
"foo".startIndex.advancedBy(4)   // fatal error: cannot increment endIndex
</code></pre>

<p>Is this to be considered a bug, or is <code>String.CharacterView.Index</code> simply not intended to be (directly) conformed to <code>Stridable</code>? </p>