apple / apple/swift-algorithms

[Bug or FeatureRequest]: collection.rotate(toStartAt: Int) crashes with negative argument

Open
#239 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
6.3k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

Replace this paragraph with a short description of the incorrect incorrect behavior. If this is a regression, please note the last version that the behavior was correct in addition to your current version.

**Swift Algorithms version:**
`1.2.0` (= main branch currently)

**Swift version:**
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

### Checklist
- [x] If possible, I've reproduced the issue using the `main` branch of this package
- [x] I've searched for [existing GitHub issues](https://github.com/apple/swift-algorithms/issues)

### Steps to Reproduce
calling .rotate(toStartAt: ) with a negative number causes a run-time error

```Swift
var array = [1, 2, 3, 4, 5, 6, 7, 8]
array.rotate(toStartAt: -2)
```

### Expected behavior
This function shouldn't crash. Considering what .rotate() does (positive values rotate elements to the left) being called with a negative value should rotate to the right.
Another option would be to only allow for positive values as arguments, e.g. `UInt` as parameter.

### Actual behavior
Code crashes with an fatal error:
`Swift/arm64e-apple-macos.swiftinterface:16236: Fatal error: Range requires lowerBound <= upperBound`

Kind regards,
Heiko

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.