fix warnings encountered with Nim 1.6 (#128)

* fix warnings encountered with Nim 1.6

* Create shims/stddefects.nim with aliases for all defects
This commit is contained in:
Miran 2022-07-27 10:10:26 +02:00 committed by GitHub
parent d37e77a72b
commit 2ab76e2cc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 15 deletions

View File

@ -37,6 +37,9 @@
## be reasonable for short-lived allocations.
##
when (NimMajor, NimMinor) < (1, 4):
import ../shims/stddefects
type
StackArray*[T] = object
bufferLen: int32
@ -50,7 +53,7 @@ else:
proc alloca(n: int): pointer {.importc, header: "<alloca.h>".}
proc raiseRangeError(s: string) =
raise newException(RangeError, s)
raise newException(RangeDefect, s)
proc raiseOutOfRange =
raiseRangeError "index out of range"

View File

@ -2,6 +2,10 @@
import ../ptrops, typetraits, hashes
when (NimMajor, NimMinor) < (1, 4):
import ../shims/stddefects
const rangesGCHoldEnabled = not defined(rangesDisableGCHold)
const unsafeAPIEnabled* = defined(rangesEnableUnsafeAPI)
@ -159,17 +163,11 @@ proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: openArray[T]) =
if L == v.len:
for i in 0..<L: r[i + a] = v[i]
else:
raise newException(RangeError, "different lengths for slice assignment")
raise newException(RangeDefect, "different lengths for slice assignment")
template toOpenArray*[T](r: Range[T]): auto =
when false:
# when (NimMajor,NimMinor,NimPatch)>=(0,19,9):
# error message in Nim HEAD 2019-01-02:
# "for a 'var' type a variable needs to be passed, but 'toOpenArray(cast[ptr UncheckedArray[T]](curHash.start), 0, high(curHash))' is immutable"
toOpenArray(cast[ptr UncheckedArray[T]](r.start), 0, r.high)
else:
# NOTE: `0` in `array[0, T]` is irrelevant
toOpenArray(cast[ptr array[0, T]](r.start)[], 0, r.high)
# NOTE: `0` in `array[0, T]` is irrelevant
toOpenArray(cast[ptr array[0, T]](r.start)[], 0, r.high)
proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: Range[T]) {.inline.} =
r[s] = toOpenArray(v)
@ -246,7 +244,7 @@ proc tryAdvance*[T](x: var MutRange[T], idx: int): bool {.inline.} =
proc advance*[T](x: var Range[T], idx: int) =
## Move internal start offset of range ``x`` by ``idx`` elements forward.
let res = x.advanceImpl(idx)
if not res: raise newException(IndexError, "Advance Error")
if not res: raise newException(IndexDefect, "Advance Error")
proc advance*[T](x: var MutRange[T], idx: int) {.inline.} =
## Move internal start offset of range ``x`` by ``idx`` elements forward.

23
stew/shims/stddefects.nim Normal file
View File

@ -0,0 +1,23 @@
when (NimMajor, NimMinor) < (1, 4):
type
AccessViolationDefect* = AccessViolationError
ArithmeticDefect* = ArithmeticError
AssertionDefect* = AssertionError
DeadThreadDefect* = DeadThreadError
DivByZeroDefect* = DivByZeroError
FieldDefect* = FieldError
FloatDivByZeroDefect* = FloatDivByZeroError
FloatInexactDefect* = FloatInexactError
FloatInvalidOpDefect* = FloatInvalidOpError
FloatOverflowDefect* = FloatOverflowError
FloatUnderflowDefect* = FloatUnderflowError
FloatingPointDefect* = FloatingPointError
IndexDefect* = IndexError
NilAccessDefect* = NilAccessError
ObjectAssignmentDefect* = ObjectAssignmentError
ObjectConversionDefect* = ObjectConversionError
OutOfMemDefect* = OutOfMemError
OverflowDefect* = OverflowError
RangeDefect* = RangeError
ReraiseDefect* = ReraiseError
StackOverflowDefect* = StackOverflowError

View File

@ -11,6 +11,9 @@ import
../../stew/ptrops,
../../stew/ranges/[stackarrays]
when (NimMajor, NimMinor) < (1, 4):
import ../../stew/shims/stddefects
suite "Stack arrays":
test "Basic operations work as expected":
var arr = allocStackArray(int, 10)
@ -33,14 +36,14 @@ suite "Stack arrays":
cast[ptr int](offset(addr arr[0], 5))[] == 10
test "Allocating with a negative size throws a RangeError":
expect RangeError:
expect RangeDefect:
discard allocStackArray(string, -1)
test "The array access is bounds-checked":
var arr = allocStackArray(string, 3)
arr[2] = "test"
check arr[2] == "test"
expect RangeError:
expect RangeDefect:
arr[3] = "another test"
test "proof of stack allocation":

View File

@ -10,6 +10,10 @@ import
unittest2, sets,
../../stew/ranges/[typedranges, ptr_arith]
when (NimMajor, NimMinor) < (1, 4):
import ../../stew/shims/stddefects
suite "Typed ranges":
test "basic stuff":
var r = newRange[int](5)
@ -169,7 +173,7 @@ suite "Typed ranges":
try:
a.advance(b)
res = 1
except IndexError:
except IndexDefect:
res = 2
res
@ -221,7 +225,7 @@ suite "Typed ranges":
try:
a.advance(b)
res = 1
except IndexError:
except IndexDefect:
res = 2
res