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:
parent
cd9af6b804
commit
7d6cbf290a
92 changed files with 323 additions and 300 deletions
|
|
@ -12,7 +12,7 @@
|
|||
##
|
||||
## None of the procs that get an individual value from the deque can be used
|
||||
## on an empty deque.
|
||||
## If compiled with `boundChecks` option, those procs will raise an `IndexError`
|
||||
## If compiled with `boundChecks` option, those procs will raise an `IndexDefect`
|
||||
## on such access. This should not be relied upon, as `-d:release` will
|
||||
## disable those checks and may return garbage or crash the program.
|
||||
##
|
||||
|
|
@ -24,7 +24,7 @@
|
|||
##
|
||||
## var a = initDeque[int]()
|
||||
##
|
||||
## doAssertRaises(IndexError, echo a[0])
|
||||
## doAssertRaises(IndexDefect, echo a[0])
|
||||
##
|
||||
## for i in 1 .. 5:
|
||||
## a.addLast(10*i)
|
||||
|
|
@ -70,7 +70,7 @@ template initImpl(result: typed, initialSize: int) =
|
|||
assert isPowerOfTwo(initialSize)
|
||||
result.mask = initialSize-1
|
||||
newSeq(result.data, initialSize)
|
||||
|
||||
|
||||
template checkIfInitialized(deq: typed) =
|
||||
when compiles(defaultInitialSize):
|
||||
if deq.mask == 0:
|
||||
|
|
@ -97,16 +97,16 @@ template emptyCheck(deq) =
|
|||
# Bounds check for the regular deque access.
|
||||
when compileOption("boundChecks"):
|
||||
if unlikely(deq.count < 1):
|
||||
raise newException(IndexError, "Empty deque.")
|
||||
raise newException(IndexDefect, "Empty deque.")
|
||||
|
||||
template xBoundsCheck(deq, i) =
|
||||
# Bounds check for the array like accesses.
|
||||
when compileOption("boundChecks"): # d:release should disable this.
|
||||
if unlikely(i >= deq.count): # x < deq.low is taken care by the Natural parameter
|
||||
raise newException(IndexError,
|
||||
raise newException(IndexDefect,
|
||||
"Out of bounds: " & $i & " > " & $(deq.count - 1))
|
||||
if unlikely(i < 0): # when used with BackwardsIndex
|
||||
raise newException(IndexError,
|
||||
raise newException(IndexDefect,
|
||||
"Out of bounds: " & $i & " < 0")
|
||||
|
||||
proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
|
||||
|
|
@ -117,7 +117,7 @@ proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
|
|||
a.addLast(10*i)
|
||||
assert a[0] == 10
|
||||
assert a[3] == 40
|
||||
doAssertRaises(IndexError, echo a[8])
|
||||
doAssertRaises(IndexDefect, echo a[8])
|
||||
|
||||
xBoundsCheck(deq, i)
|
||||
return deq.data[(deq.head + i) and deq.mask]
|
||||
|
|
@ -131,7 +131,7 @@ proc `[]`*[T](deq: var Deque[T], i: Natural): var T {.inline.} =
|
|||
a.addLast(10*i)
|
||||
assert a[0] == 10
|
||||
assert a[3] == 40
|
||||
doAssertRaises(IndexError, echo a[8])
|
||||
doAssertRaises(IndexDefect, echo a[8])
|
||||
|
||||
xBoundsCheck(deq, i)
|
||||
return deq.data[(deq.head + i) and deq.mask]
|
||||
|
|
@ -160,7 +160,7 @@ proc `[]`*[T](deq: Deque[T], i: BackwardsIndex): T {.inline.} =
|
|||
a.addLast(10*i)
|
||||
assert a[^1] == 50
|
||||
assert a[^4] == 20
|
||||
doAssertRaises(IndexError, echo a[^9])
|
||||
doAssertRaises(IndexDefect, echo a[^9])
|
||||
|
||||
xBoundsCheck(deq, deq.len - int(i))
|
||||
return deq[deq.len - int(i)]
|
||||
|
|
@ -175,7 +175,7 @@ proc `[]`*[T](deq: var Deque[T], i: BackwardsIndex): var T {.inline.} =
|
|||
a.addLast(10*i)
|
||||
assert a[^1] == 50
|
||||
assert a[^4] == 20
|
||||
doAssertRaises(IndexError, echo a[^9])
|
||||
doAssertRaises(IndexDefect, echo a[^9])
|
||||
|
||||
xBoundsCheck(deq, deq.len - int(i))
|
||||
return deq[deq.len - int(i)]
|
||||
|
|
@ -569,14 +569,14 @@ when isMainModule:
|
|||
try:
|
||||
echo deq[99]
|
||||
assert false
|
||||
except IndexError:
|
||||
except IndexDefect:
|
||||
discard
|
||||
|
||||
try:
|
||||
assert deq.len == 4
|
||||
for i in 0 ..< 5: deq.popFirst()
|
||||
assert false
|
||||
except IndexError:
|
||||
except IndexDefect:
|
||||
discard
|
||||
|
||||
# grabs some types of resize error.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue