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-14931] Lazy collection wrappers allow indices out of range #57273

Open
WowbaggersLiquidLunch opened this issue Jul 15, 2021 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@WowbaggersLiquidLunch
Copy link
Collaborator

Previous ID SR-14931
Radar rdar://problem/80820390
Original Reporter @WowbaggersLiquidLunch
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 1cd1cb3a2909ef46413eb5bf3c508fbd

Issue Description:

Lazy collection wrappers such as LazyFilterSequence allow indexing into positions that are out of bound, as long as they're in bound for the base collection, and returns the element at the given position in the base collection.

Example from a tweet by @ole:

let evens = Array(1...10)
  .lazy
  .filter { $0.isMultiple(of: 2) }
evens.indices // → [1, 3, 5, 7, 9]
evens[0]      // → 1 (exploding head emoji!)

The correct behaviour should be like that of slices:

let foo = ["a", "b", "c"]
let bar = foo[1...] // Array.SubSequence ["b", "c"]
print(bar[0]) // Fatal error: Index out of bounds
@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

2 participants