mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 17:34:43 +00:00
implements https://github.com/nim-lang/RFCs/issues/557
It inserts defect handing into a bare except branch
```nim
try:
raiseAssert "test"
except:
echo "nope"
```
=>
```nim
try:
raiseAssert "test"
except:
# New behaviov, now well-defined: **never** catches the assert, regardless of panic mode
raiseDefect()
echo "nope"
```
In this way, `except` still catches foreign exceptions, but panics on
`Defect`. Probably when Nim has `except {.foreign.}`, we can extend
`raiseDefect` to foreign exceptions as well. That's supposed to be a
small use case anyway.
`--legacy:noPanicOnExcept` is provided for a transition period.
(cherry picked from commit 26b86c8f4d)
51 lines
935 B
Nim
51 lines
935 B
Nim
discard """
|
|
cmd: "nim c -d:release --rangeChecks:on $file"
|
|
disabled: "windows"
|
|
output: '''StrictPositiveRange
|
|
float
|
|
range fail expected
|
|
range fail expected
|
|
'''
|
|
"""
|
|
import math, fenv
|
|
|
|
type
|
|
Positive = range[0.0..Inf]
|
|
StrictPositive = range[minimumPositiveValue(float)..Inf]
|
|
Negative32 = range[-maximumPositiveValue(float32) .. -1.0'f32]
|
|
|
|
proc myoverload(x: float) =
|
|
echo "float"
|
|
|
|
proc myoverload(x: Positive) =
|
|
echo "PositiveRange"
|
|
|
|
proc myoverload(x: StrictPositive) =
|
|
echo "StrictPositiveRange"
|
|
|
|
let x = 9.0.StrictPositive
|
|
myoverload(x)
|
|
myoverload(9.0)
|
|
|
|
doAssert(sqrt(x) == 3.0)
|
|
|
|
var z = -10.0
|
|
try:
|
|
myoverload(StrictPositive(z))
|
|
except Exception:
|
|
echo "range fail expected"
|
|
|
|
|
|
proc strictOnlyProc(x: StrictPositive): bool =
|
|
if x > 1.0: true else: false
|
|
|
|
let x2 = 5.0.Positive
|
|
doAssert(strictOnlyProc(x2))
|
|
|
|
try:
|
|
let x4 = 0.0.Positive
|
|
discard strictOnlyProc(x4)
|
|
except Exception:
|
|
echo "range fail expected"
|
|
|