Skip to content
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-2016] stride(from:0 as UInt8, though: 255, by: 2) should not trap. #44626

Closed
moiseev mannequin opened this issue Jul 7, 2016 · 7 comments
Closed

[SR-2016] stride(from:0 as UInt8, though: 255, by: 2) should not trap. #44626

moiseev mannequin opened this issue Jul 7, 2016 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@moiseev
Copy link
Mannequin

moiseev mannequin commented Jul 7, 2016

Previous ID SR-2016
Radar None
Original Reporter @moiseev
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug
Assignee @xwu
Priority Medium

md5: 568e368fa049264e9121cf3adc674d32

is duplicated by:

  • SR-13883 StrideToIterator and StrideThroughIterator are broken

relates to:

  • SR-14325 Support Strideable on finite enums.

Issue Description:

Right now it attempts to increment the current value beyond the bounds of UInt8, even though it is clear that the value will be greater than the stride's end.

@Dante-Broggi
Copy link
Contributor

I think this would require an `x.advanced(by: Stride, limitedBy: Self) -> Self?` customization point in `Strideable`, and for the `stride(to:by: )` and `stride(through:by: )` iterators to use it.

@moiseev
Copy link
Mannequin Author

moiseev mannequin commented Jul 17, 2018

Not necessarily. It should be possible to check the distance to end before calling _step, and if it's less than stride simply return end.

@xwu
Copy link
Collaborator

xwu commented Mar 10, 2021

Hi @gonzalolarralde, so that you're aware, I've got a PR that's pending a bit of revision which addresses this bug:

#34860

@gonzalolarralde
Copy link
Contributor

@xwu Thanks for the pointer! you saved me a ton of time 🙂 I was drafting a solution that looks similar in principle to yours to then try to figure out how to adapt it to make it fit the API stability story.
Counter-example showing `distance(to: )`'s limitation make it clear that this might look like a simpler issue in principle than what it really is. (striking after re-reading your comments in the PR)

@xwu
Copy link
Collaborator

xwu commented Mar 10, 2021

Sure thing. I'll find some time to get the fix in my PR merged this week. I've been putting off updating the documentation comments but that can be done separately.

@gonzalolarralde
Copy link
Contributor

perfect, thanks @xwu . TBH I got into this issue because I was hoping to be able to use Strideable on a finite enum. Something like this would exemplify:

enum E : Int, Strideable {
  case one = 1, two, three, four
  func distance(to other: Self) -> Int {
    return other.rawValue - self.rawValue
  }
  func advanced(by n: Int) -> Self {
    return Self(rawValue: self.rawValue + n)!
  }
}

// current implementation requires this to past beyond `Test.four` to know it's finished.
print(Array(stride(from: Test.one, to: Test.four, by: 2)))

so I attempted a naive fix that I wanted to verify how could be translated into something stable to the current implementation's inline methods. It is available here: gonzalolarralde@b846dba

I opened SR-14325 to track the use-case I was hoping to enable, if you have any ideas/pointers on how a API-stable solution would look I would really appreciate your input!

@xwu
Copy link
Collaborator

xwu commented Mar 11, 2021

Fixed in PR#34860.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants