Error -> Defect for defects (#13908)

* 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.
This commit is contained in:
Jacek Sieka 2020-04-28 19:56:01 +02:00 • committed by GitHub
commit 7d6cbf290a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
92 changed files with 323 additions and 300 deletions

View file

@ -154,16 +154,16 @@ type
x1: string
case kind1: bool
of false: y1: string
of true:
of true:
y2: seq[string]
case kind2: bool
of true: z1: string
of false:
of false:
z2: seq[string]
flag: bool
x2: string
proc test_myobject =
proc test_myobject =
var x: MyObject
x.x1 = "x1"
x.x2 = "x2"
@ -183,7 +183,7 @@ proc test_myobject =
try:
x.kind1 = x.flag # flag is not accesible
except FieldError:
except FieldDefect:
echo "prevented"
doAssert(x.x1 == "x1")

View file

@ -10,7 +10,7 @@ proc callC() = callA()
try:
callC()
except AssertionError:
except AssertionDefect:
write(stdout, "assertion failure!")
except:
write(stdout, "unknown exception!")

View file

@ -18,12 +18,12 @@ type
try:
doAssert(false, "msg1") # doAssert test
except AssertionError as e:
except AssertionDefect as e:
assert e.msg.endsWith "tassert2.nim(20, 11) `false` msg1"
try:
assert false # assert test with no msg
except AssertionError as e:
except AssertionDefect as e:
assert e.msg.endsWith "tassert2.nim(25, 10) `false` "
try:
@ -31,13 +31,13 @@ try:
doAssert(a+a==1) # assert test with Ast expression
# BUG: const folding would make "1+1==1" appear as `false` in
# assert message
except AssertionError as e:
except AssertionDefect as e:
assert e.msg.endsWith "`a + a == 1` "
try:
let a = 1
doAssert a+a==1 # ditto with `doAssert` and no parens
except AssertionError as e:
except AssertionDefect as e:
assert e.msg.endsWith "`a + a == 1` "
proc fooStatic() =
@ -91,14 +91,14 @@ block: ## checks for issue https://github.com/nim-lang/Nim/issues/8518
try:
doAssert fun("foo1") == fun("foo2"), "mymsg"
except AssertionError as e:
except AssertionDefect as e:
# used to expand out the template instantiaiton, sometimes filling hundreds of lines
assert e.msg.endsWith ""
block: ## checks for issue https://github.com/nim-lang/Nim/issues/9301
try:
doAssert 1 + 1 == 3
except AssertionError as e:
except AssertionDefect as e:
# used to const fold as false
assert e.msg.endsWith "tassert2.nim(100, 14) `1 + 1 == 3` "
@ -106,6 +106,6 @@ block: ## checks AST isn't transformed as it used to
let a = 1
try:
doAssert a > 1
except AssertionError as e:
except AssertionDefect as e:
# used to rewrite as `1 < a`
assert e.msg.endsWith "tassert2.nim(108, 14) `a > 1` "

View file

@ -33,7 +33,7 @@ try:
proc foo() =
assert(false)
foo()
except AssertionError:
except AssertionDefect:
let e = getCurrentException()
let trace = e.getStackTrace
if tmatch(trace, expected): echo true else: echo "wrong trace:\n" & trace

View file

@ -14,7 +14,7 @@ import asyncdispatch, strutils
proc foobar() {.async.} =
if 5 == 5:
raise newException(IndexError, "Test")
raise newException(IndexDefect, "Test")
proc catch() {.async.} =
# TODO: Create a test for when exceptions are not caught.
@ -25,26 +25,26 @@ proc catch() {.async.} =
try:
await foobar()
except IndexError:
except IndexDefect:
echo("Specific except")
try:
await foobar()
except OSError, FieldError, IndexError:
except OSError, FieldDefect, IndexDefect:
echo("Multiple idents in except")
try:
await foobar()
except OSError, FieldError:
except OSError, FieldDefect:
assert false
except IndexError:
except IndexDefect:
echo("Multiple except branches")
try:
await foobar()
except IndexError:
except IndexDefect:
echo("Multiple except branches 2")
except OSError, FieldError:
except OSError, FieldDefect:
assert false
waitFor catch()

View file

@ -45,7 +45,7 @@ when defined(upcoming):
poll()
proc eventTest5298() =
# Event must raise `AssertionError` if event was unregistered twice.
# Event must raise `AssertionDefect` if event was unregistered twice.
# Issue #5298.
let e = newAsyncEvent()
var eventReceived = false
@ -57,7 +57,7 @@ when defined(upcoming):
poll()
try:
e.unregister()
except AssertionError:
except AssertionDefect:
discard
e.close()

View file

@ -2,7 +2,7 @@ import compiler/unittest_light
proc testAssertEquals() =
assertEquals("foo", "foo")
doAssertRaises(AssertionError):
doAssertRaises(AssertionDefect):
assertEquals("foo", "foo ")
proc testMismatch() =

View file

@ -78,7 +78,7 @@ proc getFirstValue[K,V](m : Map[K,V]): V =
for i in m.valuesSeq:
return i
raise newException(RangeError, "no values")
raise newException(RangeDefect, "no values")
proc useConceptProcInGeneric[K, V](t: Table[K, V]): V =
return t.getFirstValue

View file

@ -13,7 +13,7 @@ proc main() =
try:
echo "before"
raise newException(CatchableError, "foo")
except AssertionError:
except AssertionDefect:
echo "caught"
echo "after"
@ -27,7 +27,7 @@ proc mainB() =
echo "before"
raise newException(CatchableError, "foo")
# except CatchableError: # would work
except AssertionError:
except AssertionDefect:
echo "caught"
except:
raise

View file

@ -2,7 +2,7 @@ discard """
cmd: "nim c --gc:arc --exceptions:goto --panics:off $file"
output: '''prevented!
caught
AssertionError
AssertionDefect
900'''
"""
@ -30,18 +30,18 @@ proc main(i: int) =
try:
var objA = ObjA()
bplease(ObjB(objA))
except ObjectConversionError:
except ObjectConversionDefect:
echo "prevented!"
try:
takeRange(i)
except RangeError:
except RangeDefect:
echo "caught"
try:
helper()
except AssertionError:
echo "AssertionError"
except AssertionDefect:
echo "AssertionDefect"
echo i * i

View file

@ -1,5 +1,5 @@
discard """
outputsub: "index 2 not in 0 .. 0 [IndexError]"
outputsub: "index 2 not in 0 .. 0 [IndexDefect]"
exitcode: 1
cmd: "nim c --gc:arc --exceptions:setjmp $file"
"""

View file

@ -1,5 +1,5 @@
discard """
errormsg: "can raise an unlisted exception: ref FloatingPointError"
errormsg: "can raise an unlisted exception: ref FloatingPointDefect"
line: 10
"""
@ -7,7 +7,7 @@ proc foo() {.raises: [].} =
try:
discard
except KeyError:
raise newException(FloatingPointError, "foo")
raise newException(FloatingPointDefect, "foo")
except Exception:
discard

View file

@ -89,10 +89,10 @@ block:
result.add(1212)
try:
try:
raise newException(AssertionError, "a")
raise newException(AssertionDefect, "a")
finally:
result.add(42)
except AssertionError:
except AssertionDefect:
result.add(99)
finally:
result.add(10)
@ -104,7 +104,7 @@ block:
result.add(-1)
except ValueError:
result.add(-1)
except IndexError:
except IndexDefect:
result.add(2)
except:
result.add(3)

View file

@ -4,5 +4,5 @@ discard """
"""
try:
raise newException(ValueError, "")
except OverflowError:
except OverflowDefect:
echo("Error caught")

View file

@ -1,5 +1,5 @@
discard """
outputsub: "Error: unhandled exception: FPU operation caused an overflow [FloatOverflowError]"
outputsub: "Error: unhandled exception: FPU operation caused an overflow [FloatOverflowDefect]"
exitcode: "1"
"""
# Test new floating point exceptions

View file

@ -1,5 +1,5 @@
discard """
outputsub: "Error: unhandled exception: FPU operation caused a NaN result [FloatInvalidOpError]"
outputsub: "Error: unhandled exception: FPU operation caused a NaN result [FloatInvalidOpDefect]"
exitcode: "1"
"""
# Test new floating point exceptions

View file

@ -2,7 +2,7 @@
proc foo =
var x: seq[int]
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
inc x[0]
foo()

View file

@ -24,7 +24,7 @@ var obj = X(f0: "foo", f1: C, f3: 3.14)
block:
echo obj.f0
echo obj.f1
doAssertRaises(FieldError): echo obj.f2
doAssertRaises(FieldDefect): echo obj.f2
echo obj.f3
block:
@ -32,7 +32,7 @@ block:
echo a0[]
# let a1 = unsafeAddr(obj.f1)
# echo a1[]
doAssertRaises(FieldError):
doAssertRaises(FieldDefect):
let a2 = addr(obj.f2)
echo a2[]
let a3 = addr(obj.f3)

View file

@ -251,8 +251,8 @@ proc parse_parameter(quit_on_failure: bool, param, value: string,
case param_kind:
of PK_INT:
try: result.int_val = value.parseInt
except OverflowError:
raise_or_quit(OverflowError, ("parameter $1 requires an " &
except OverflowDefect:
raise_or_quit(OverflowDefect, ("parameter $1 requires an " &
"integer, but $2 is too large to fit into one") % [param,
escape(value)])
except ValueError:

View file

@ -11,7 +11,7 @@ type
proc toNumber[T: int|uint|int64|uint64](v: ResultValue): T =
if v < low(T) or v > high(T):
raise newException(RangeError, "protocol error")
raise newException(RangeDefect, "protocol error")
return T(v)
#proc toNumber[T](v: int32): T =

View file

@ -1,5 +1,5 @@
discard """
outputsub: '''Error: unhandled exception: cannot dispatch; dispatcher is nil [NilAccessError]'''
outputsub: '''Error: unhandled exception: cannot dispatch; dispatcher is nil [NilAccessDefect]'''
exitcode: 1
"""
# bug #5599

View file

@ -14,14 +14,14 @@ var x: range[1..10] = 10
try:
x += 1
echo x
except OverflowError, RangeError:
except OverflowDefect, RangeDefect:
expected += 1
echo x
try:
inc x
echo x
except OverflowError, RangeError:
except OverflowDefect, RangeDefect:
expected += 1
echo x
@ -29,14 +29,14 @@ x = 1
try:
x -= 1
echo x
except OverflowError, RangeError:
except OverflowDefect, RangeDefect:
expected += 1
echo x
try:
dec x
echo x
except OverflowError, RangeError:
except OverflowDefect, RangeDefect:
expected += 1
echo x

View file

@ -2,25 +2,25 @@ discard """
action: run
"""
doAssertRaises(OverflowError): discard low(int8) - 1'i8
doAssertRaises(OverflowError): discard high(int8) + 1'i8
doAssertRaises(OverflowError): discard abs(low(int8))
doAssertRaises(DivByZeroError): discard 1 mod 0
doAssertRaises(DivByZeroError): discard 1 div 0
doAssertRaises(OverflowError): discard low(int8) div -1'i8
doAssertRaises(OverflowDefect): discard low(int8) - 1'i8
doAssertRaises(OverflowDefect): discard high(int8) + 1'i8
doAssertRaises(OverflowDefect): discard abs(low(int8))
doAssertRaises(DivByZeroDefect): discard 1 mod 0
doAssertRaises(DivByZeroDefect): discard 1 div 0
doAssertRaises(OverflowDefect): discard low(int8) div -1'i8
doAssertRaises(OverflowError): discard -low(int64)
doAssertRaises(OverflowError): discard low(int64) - 1'i64
doAssertRaises(OverflowError): discard high(int64) + 1'i64
doAssertRaises(OverflowDefect): discard -low(int64)
doAssertRaises(OverflowDefect): discard low(int64) - 1'i64
doAssertRaises(OverflowDefect): discard high(int64) + 1'i64
type E = enum eA, eB
doAssertRaises(OverflowError): discard eA.pred
doAssertRaises(OverflowError): discard eB.succ
doAssertRaises(OverflowDefect): discard eA.pred
doAssertRaises(OverflowDefect): discard eB.succ
doAssertRaises(OverflowError): discard low(int8) * -1
doAssertRaises(OverflowError): discard low(int64) * -1
doAssertRaises(OverflowError): discard high(int8) * 2
doAssertRaises(OverflowError): discard high(int64) * 2
doAssertRaises(OverflowDefect): discard low(int8) * -1
doAssertRaises(OverflowDefect): discard low(int64) * -1
doAssertRaises(OverflowDefect): discard high(int8) * 2
doAssertRaises(OverflowDefect): discard high(int64) * 2
doAssert abs(-1) == 1
doAssert 2 div 2 == 1

View file

@ -1,8 +1,8 @@
block: # bug #2427
var x = 0'u8
dec x # OverflowError
x -= 1 # OverflowError
dec x # OverflowDefect
x -= 1 # OverflowDefect
x = x - 1 # No error
doAssert(x == 253'u8)

View file

@ -179,13 +179,13 @@ for i in list4.low .. list4.high:
echo(list4[i])
var list5: seq[int] = @[]
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
list5[0] = 1
list5.add(1)
assert list5[0] == 1
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
echo list5[42]
# -- Errata: var list: seq[int]; echo(list[0]). This now creates an exception,

View file

@ -22,7 +22,7 @@ block:
assert obj.contents == "Hello"
var obj2 = Box(empty: true)
doAssertRaises(FieldError):
doAssertRaises(FieldDefect):
echo(obj2.contents)
import json

View file

@ -1,5 +1,5 @@
discard """
outputsub: '''ObjectAssignmentError'''
outputsub: '''ObjectAssignmentDefect'''
exitcode: "1"
"""

View file

@ -13,7 +13,7 @@ var
try:
writeLine(stdout, b - a)
except OverflowError:
except OverflowDefect:
overflowDetected = true
{.pop.} # overflow check
@ -33,7 +33,7 @@ block: # Overflow checks in a proc
try:
foo()
except OverflowError:
except OverflowDefect:
overflowDetected = true
doAssert(overflowDetected)
@ -53,7 +53,7 @@ block: # Overflow checks in a forward declared proc
try:
foo()
except OverflowError:
except OverflowDefect:
overflowDetected = true
doAssert(overflowDetected)
@ -73,7 +73,7 @@ block: # Overflow checks doesn't affect fwd declaration
try:
foo()
except OverflowError:
except OverflowDefect:
overflowDetected = true
doAssert(not overflowDetected)

View file

@ -1,5 +1,5 @@
discard """
outputsub: "Error: unhandled exception: over- or underflow [OverflowError]"
outputsub: "Error: unhandled exception: over- or underflow [OverflowDefect]"
exitcode: "1"
"""
var a : int32 = 2147483647

View file

@ -1,5 +1,5 @@
discard """
outputsub: "value out of range: 50 notin 0 .. 40 [RangeError]"
outputsub: "value out of range: 50 notin 0 .. 40 [RangeDefect]"
exitcode: "1"
"""

View file

@ -1,5 +1,5 @@
discard """
outputsub: "value out of range: 50 notin 0 .. 40 [RangeError]"
outputsub: "value out of range: 50 notin 0 .. 40 [RangeDefect]"
exitcode: "1"
"""

View file

@ -10,5 +10,5 @@ var
try:
var
r = initRational(1, 0) # this fails - no zero denominator
except AssertionError:
except AssertionDefect:
echo "true"

View file

@ -16,7 +16,7 @@ suite "replace":
check("123".replace(re"(?<foo>\d)(\d)", "${foo}$#$#") == "1123")
test "replacing missing captures should throw instead of segfaulting":
expect IndexError: discard "ab".replace(re"(a)|(b)", "$1$2")
expect IndexError: discard "b".replace(re"(a)?(b)", "$1$2")
expect IndexDefect: discard "ab".replace(re"(a)|(b)", "$1$2")
expect IndexDefect: discard "b".replace(re"(a)?(b)", "$1$2")
expect KeyError: discard "b".replace(re"(a)?", "${foo}")
expect KeyError: discard "b".replace(re"(?<foo>a)?", "${foo}")

View file

@ -266,7 +266,7 @@ block: # not ready for vm because exception is compile error
var i = 32
v.setBit(i)
doAssert false
except RangeError:
except RangeDefect:
discard
except:
doAssert false

View file

@ -43,7 +43,7 @@ block tcount:
# bubble sort
for i in low(arr)..high(arr):
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeError]
for j in i+1..high(arr): # Error: unhandled exception: value out of range: 5 [RangeDefect]
if arr[i] > arr[j]:
let tmp = arr[i]
arr[i] = arr[j]

View file

@ -169,7 +169,7 @@ block tsegfaults:
raise newException(ValueError, "not a crash")
except ValueError:
discard
except NilAccessError:
except NilAccessDefect:
echo "caught a crash!"
for i in 0..5:
main()
@ -212,7 +212,7 @@ block tsplit2:
try:
discard "hello".split("")
echo "false"
except AssertionError:
except AssertionDefect:
echo "true"

View file

@ -52,7 +52,7 @@ proc test_string_slice() =
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
# bug #6223
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
discard s[0..999]
echo("OK")

View file

@ -11,7 +11,7 @@ import macros
template rejectParse(e) =
try:
discard e
raise newException(AssertionError, "This was supposed to fail: $#!" % astToStr(e))
raise newException(AssertionDefect, "This was supposed to fail: $#!" % astToStr(e))
except ValueError: discard
proc testStrip() =

View file

@ -626,8 +626,8 @@ suite "ttimes":
doAssert a.month.Month == cast[Month](0)
doAssert a.monthday == 0
doAssertRaises(AssertionError): discard getDayOfWeek(a.monthday, a.month, a.year)
doAssertRaises(AssertionError): discard a.toTime
doAssertRaises(AssertionDefect): discard getDayOfWeek(a.monthday, a.month, a.year)
doAssertRaises(AssertionDefect): discard a.toTime
test "inX procs":
doAssert initDuration(seconds = 1).inSeconds == 1

View file

@ -56,7 +56,7 @@ proc defectiveRobot() =
of 3: raise newException(IOError, "I can't do that Dave.")
else: assert 2 + 2 == 5
test "unittest expect":
expect IOError, OSError, ValueError, AssertionError:
expect IOError, OSError, ValueError, AssertionDefect:
defectiveRobot()
var

View file

@ -68,8 +68,8 @@ proc main() =
# shows that checkNotZero won't be called if a nil is found earlier in chain
doAssert ?.a.x1.checkNotZero == 0.0
# checks that a chain without nil but with an empty seq still throws IndexError
doAssertRaises(IndexError): discard ?.a2.x8[3]
# checks that a chain without nil but with an empty seq still throws IndexDefect
doAssertRaises(IndexDefect): discard ?.a2.x8[3]
# make sure no double evaluation bug
doAssert witness == 0

View file

@ -81,13 +81,13 @@ foo(toOpenArray(seqq, 1, 3))
# empty openArray issue #7904
foo(toOpenArray(seqq, 0, -1))
foo(toOpenArray(seqq, 1, 0))
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
foo(toOpenArray(seqq, 0, -2))
foo(toOpenArray(arr, 9, 8))
foo(toOpenArray(arr, 0, -1))
foo(toOpenArray(arr, 1, 0))
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
foo(toOpenArray(arr, 10, 8))
# test openArray of openArray
@ -106,11 +106,11 @@ var arrNeg: array[-3 .. -1, int] = [1, 2, 3]
foo(toOpenArray(arrNeg, -3, -1))
foo(toOpenArray(arrNeg, 0, -1))
foo(toOpenArray(arrNeg, -3, -4))
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
foo(toOpenArray(arrNeg, -4, -1))
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
foo(toOpenArray(arrNeg, -1, 0))
doAssertRaises(IndexError):
doAssertRaises(IndexDefect):
foo(toOpenArray(arrNeg, -1, -3))
doAssertRaises(Exception):
raise newException(Exception, "foo")
@ -118,9 +118,9 @@ doAssertRaises(Exception):
block:
var didThrow = false
try:
doAssertRaises(IndexError): # should fail since it's wrong exception
raise newException(FieldError, "foo")
except AssertionError:
doAssertRaises(IndexDefect): # should fail since it's wrong exception
raise newException(FieldDefect, "foo")
except AssertionDefect:
# ok, throwing was correct behavior
didThrow = true
doAssert didThrow

View file

@ -1,6 +1,6 @@
discard """
targets: "c cpp"
outputsub: '''ObjectAssignmentError'''
outputsub: '''ObjectAssignmentDefect'''
exitcode: "1"
"""

View file

@ -1,6 +1,6 @@
discard """
targets: "c cpp"
outputsub: '''ObjectAssignmentError'''
outputsub: '''ObjectAssignmentDefect'''
exitcode: "1"
"""

View file

@ -1,6 +1,6 @@
discard """
targets: "c cpp"
outputsub: '''ObjectAssignmentError'''
outputsub: '''ObjectAssignmentDefect'''
exitcode: "1"
"""