From f3ecc15a94c12b149f0665d250af1d71dd128721 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 6 Dec 2018 15:42:44 -0800 Subject: refs #9880 show index and bound in lots of `index out of bounds` errors --- lib/pure/collections/sharedstrings.nim | 6 ++++-- lib/pure/subexes.nim | 4 ++-- lib/system/chcks.nim | 9 ++++++++- lib/system/helpers2.nim | 5 +++++ 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 lib/system/helpers2.nim (limited to 'lib') diff --git a/lib/pure/collections/sharedstrings.nim b/lib/pure/collections/sharedstrings.nim index 7e9de4b73..b283cd4b1 100644 --- a/lib/pure/collections/sharedstrings.nim +++ b/lib/pure/collections/sharedstrings.nim @@ -12,6 +12,8 @@ type UncheckedCharArray = UncheckedArray[char] +import system/helpers2 + type Buffer = ptr object refcount: int @@ -49,11 +51,11 @@ proc len*(s: SharedString): int = s.len proc `[]`*(s: SharedString; i: Natural): char = if i < s.len: result = s.buffer.data[i+s.first] - else: raise newException(IndexError, "index out of bounds") + else: raise newException(IndexError, formatErrorIndexBound(i, s.len-1)) proc `[]=`*(s: var SharedString; i: Natural; value: char) = if i < s.len: s.buffer.data[i+s.first] = value - else: raise newException(IndexError, "index out of bounds") + else: raise newException(IndexError, formatErrorIndexBound(i, s.len-1)) proc `[]`*(s: SharedString; ab: HSlice[int, int]): SharedString = #incRef(src.buffer) diff --git a/lib/pure/subexes.nim b/lib/pure/subexes.nim index d103af710..638e71f04 100644 --- a/lib/pure/subexes.nim +++ b/lib/pure/subexes.nim @@ -17,7 +17,7 @@ from strutils import parseInt, cmpIgnoreStyle, Digits include "system/inclrtl" - +import system/helpers2 proc findNormalized(x: string, inArray: openarray[string]): int = var i = 0 @@ -85,7 +85,7 @@ proc getFormatArg(p: var FormatParser, a: openArray[string]): int = result = parseInt(a[result])-1 else: raiseInvalidFormat("'#', '$', number or identifier expected") - if result >=% a.len: raiseInvalidFormat("index out of bounds: " & $result) + if result >=% a.len: raiseInvalidFormat(formatErrorIndexBound(result, a.len)) p.i = i proc scanDollar(p: var FormatParser, a: openarray[string], s: var string) {. diff --git a/lib/system/chcks.nim b/lib/system/chcks.nim index d3651f659..6f4e8ce37 100644 --- a/lib/system/chcks.nim +++ b/lib/system/chcks.nim @@ -8,6 +8,7 @@ # # Implementation of some runtime checks. +import system/helpers2 proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} = when hostOS == "standalone": @@ -15,6 +16,12 @@ proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} = else: sysFatal(RangeError, "value out of range: ", $val) +proc raiseIndexError3(i, a, b: int) {.compilerproc, noinline.} = + sysFatal(IndexError, formatErrorIndexBound(i, a, b)) + +proc raiseIndexError2(i, n: int) {.compilerproc, noinline.} = + sysFatal(IndexError, formatErrorIndexBound(i, n)) + proc raiseIndexError() {.compilerproc, noinline.} = sysFatal(IndexError, "index out of bounds") @@ -25,7 +32,7 @@ proc chckIndx(i, a, b: int): int = if i >= a and i <= b: return i else: - raiseIndexError() + raiseIndexError3(i, a, b) proc chckRange(i, a, b: int): int = if i >= a and i <= b: diff --git a/lib/system/helpers2.nim b/lib/system/helpers2.nim new file mode 100644 index 000000000..1c9e7c068 --- /dev/null +++ b/lib/system/helpers2.nim @@ -0,0 +1,5 @@ +template formatErrorIndexBound*[T](i, a, b: T): string = + "index out of bounds: (a:" & $a & ") <= (i:" & $i & ") <= (b:" & $b & ") " + +template formatErrorIndexBound*[T](i, n: T): string = + "index out of bounds: (i:" & $i & ") <= (n:" & $n & ") " -- cgit 1.4.1-2-gfad0