Is there a bug in stride(from:through:by:) or is it me?

Consider this:

let r = stride(from: 5, through: 1, by: -1)
print(r.contains(3)) // false

Pass it through an array and now it works:

let r = stride(from: 5, through: 1, by: -1).map { $0 }
print(r.contains(3)) // true

Going forward also works:

let r = stride(from: 1, through: 5, by: 1)
print(r.contains(3)) // true

Doesn't make any sense to me. Am I misunderstanding how the stride function works?
Using the latest Xcode 13 with Swift 5.5.
Thanks!

3 Likes

Yeah, this looks like a bug: StrideThrough implements a custom override of _customContainsEquatableElement that assumes that the start is always less than the end:

  @inlinable
  public func _customContainsEquatableElement(
    _ element: Element
  ) -> Bool? {
    if element < _start || _end < element {
      return false
    }
    return nil
  }
}
3 Likes

Please file an issue on bugs.swift.org and post the number here.

Incidentally, StrideTo is affected by the same bug.

Oof. Yes, @seanmrich, please file a bug and link here. I think the following PR will fix it but regardless it’ll be good to have a bug to reference.

Bug filed: SR-15384

4 Likes

Looks like a fix was submitted same day. Great job @xwu!