* Error -> Defect for defects The distinction between Error and Defect is subjective, context-dependent and somewhat arbitrary, so when looking at an exception, it's hard to guess what it is - this happens often when looking at a `raises` list _without_ opening the corresponding definition and digging through layers of inheritance. With the help of a little consistency in naming, it's at least possible to start disentangling the two error types and the standard lib can set a good example here.
82 lines
1.2 KiB
Nim
82 lines
1.2 KiB
Nim
discard """
|
|
output: "ok"
|
|
cmd: "nim $target --overflowChecks:off $options $file"
|
|
"""
|
|
# Tests nim's ability to detect overflows
|
|
|
|
{.push overflowChecks: on.}
|
|
|
|
var
|
|
a = high(int)
|
|
b = -2
|
|
overflowDetected = false
|
|
|
|
try:
|
|
writeLine(stdout, b - a)
|
|
except OverflowDefect:
|
|
overflowDetected = true
|
|
|
|
{.pop.} # overflow check
|
|
|
|
doAssert(overflowDetected)
|
|
|
|
block: # Overflow checks in a proc
|
|
var
|
|
a = high(int)
|
|
b = -2
|
|
overflowDetected = false
|
|
|
|
{.push overflowChecks: on.}
|
|
proc foo() =
|
|
let c = b - a
|
|
{.pop.}
|
|
|
|
try:
|
|
foo()
|
|
except OverflowDefect:
|
|
overflowDetected = true
|
|
|
|
doAssert(overflowDetected)
|
|
|
|
block: # Overflow checks in a forward declared proc
|
|
var
|
|
a = high(int)
|
|
b = -2
|
|
overflowDetected = false
|
|
|
|
proc foo()
|
|
|
|
{.push overflowChecks: on.}
|
|
proc foo() =
|
|
let c = b - a
|
|
{.pop.}
|
|
|
|
try:
|
|
foo()
|
|
except OverflowDefect:
|
|
overflowDetected = true
|
|
|
|
doAssert(overflowDetected)
|
|
|
|
block: # Overflow checks doesn't affect fwd declaration
|
|
var
|
|
a = high(int)
|
|
b = -2
|
|
overflowDetected = false
|
|
|
|
{.push overflowChecks: on.}
|
|
proc foo()
|
|
{.pop.}
|
|
|
|
proc foo() =
|
|
let c = b - a
|
|
|
|
try:
|
|
foo()
|
|
except OverflowDefect:
|
|
overflowDetected = true
|
|
|
|
doAssert(not overflowDetected)
|
|
|
|
|
|
echo "ok"
|