Swift: for i in 0..<Int8(-1) { println(i) } includes wrong values in range, silently overflows and wraps
| Originator: | jeremyw.sherman | ||
| Number: | rdar://17581456 | Date Originated: | 07-Jul-2014 06:21 PM |
| Status: | Open | Resolved: | Closed as duplicate of: 16254937 |
| Product: | Developer Tools | Product Version: | |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
When endIndex is less than startIndex,
Rather than iterating the values from startIndex to endIndex,
Instead the range generator always adds 1 to startIndex.
The generator silently overflows and wraps around before reaching the endIndex.
Steps to Reproduce:
for i in 0..<Int8(-1) { println(i) }
Expected Results:
The only value printed is "0", consistent with range [0, -1) including only the value 0.
The values in the range are iterated, from startIndex up to but not including endIndex.
Iteration should not be able to advance beyond the bounds of the underlying integral type. If nothing else, Swift's overflow-safety should cause an exception when we hit the end of the representable values.
Actual Results:
The values printed are: 0, 1, …, 127, -128, -127, …, -2.
The generator's next() function always returns the current value + 1, rather than + or - 1 per the direction of the range.
Overflow is silent.
(Issuing a for-in print gets really ugly when the int-type is 64 bits.)
Version:
Xcode 6.0
Build version 6A254o
ProductName: Mac OS X
ProductVersion: 10.9.4
BuildVersion: 13E28
Swift version 1.0 (swift-600.0.38.7)
Target: x86_64-apple-darwin13.3.0
Notes:
Likely related to <rdar://17559710>.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!