Range init runtime crash with @const literal value

I get a crash in runtime with this code:

let limit: ClosedRange<Int> = 1...0 // Error: Execution was interrupted, reason: EXC_BREAKPOINT

Is it expected? I didn't found any discussions or information in One-sided Ranges proposal. There are also no replies here: Swift: range crash | Apple Developer Forums
I assumed (naively?) that incorrect literal bounds would be checked at compile time.

the Comparable....(_:_:) operator is documented to have the precondition minimum <= maximum.

they really should be, but i assume the compiler teams just haven’t gotten around to it yet.

Thanks for highlighting this information.

I met this error trying to make a build-Time Constant (Build-Time Constant Values).
The following supported Types are mentioned in the proposal:

  • Certain standard library types that are expressible with literal values
  • Integer and Floating-Point types ((U)?Int(\d*), Float, Double, Half), String

My assumption about compile checking was based on

  • the fact thats integer literals are already checked for overflow
  • Certain standard library types are mentioned in the proposal and Ranges are a family of common stdlib Types.
  • reasonings that build-Time Constants (and literals particulary) are known at compile and therefore can't be invalid if the code compiled.

In fact, _const let range: ClosedRange<UInt> = 1...0 is successfully compiled but crashes at runtime.

Now I'm wondering should it be

  • reported as a Bug
  • handled in "Build-Time Constant Values" review to be taken into account as part of Constant Values implementation
  • formed as a separate pitch
2 Likes