summary refs log tree commit diff stats
path: root/lib/core
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2019-02-10 13:07:11 -0800
committerAndreas Rumpf <rumpf_a@web.de>2019-02-10 22:07:11 +0100
commit491060839473fbe5fa17b4fd37b93f5dcc662db8 (patch)
tree23e04bdbc7472b13cef1bd4c60334ad8cbe78ef9 /lib/core
parent352b52a0c9f24ec82b2573a78cd65c0c19b6d29f (diff)
downloadNim-491060839473fbe5fa17b4fd37b93f5dcc662db8.tar.gz
revive #10228 (fix #9880) (#10610)
* Make index out of bounds more useful by including the 'bounds'.
* fixes #9880 index out of bounds (remaining cases); revives #10228
* change err msg to: `index 3 not in 0 .. 1`
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/typeinfo.nim10
1 files changed, 6 insertions, 4 deletions
diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim
index 32fedd0c1..d6dd16b54 100644
--- a/lib/core/typeinfo.nim
+++ b/lib/core/typeinfo.nim
@@ -27,6 +27,8 @@
 include "system/inclrtl.nim"
 include "system/hti.nim"
 
+import system/indexerrors
+
 {.pop.}
 
 type
@@ -201,14 +203,14 @@ proc `[]`*(x: Any, i: int): Any =
   of tyArray:
     var bs = x.rawType.base.size
     if i >=% x.rawType.size div bs:
-      raise newException(IndexError, "index out of bounds")
+      raise newException(IndexError, formatErrorIndexBound(i, x.rawType.size div bs))
     return newAny(x.value +!! i*bs, x.rawType.base)
   of tySequence:
     var s = cast[ppointer](x.value)[]
     if s == nil: raise newException(ValueError, "sequence is nil")
     var bs = x.rawType.base.size
     if i >=% cast[PGenSeq](s).len:
-      raise newException(IndexError, "index out of bounds")
+      raise newException(IndexError, formatErrorIndexBound(i, cast[PGenSeq](s).len-1))
     return newAny(s +!! (GenericSeqSize+i*bs), x.rawType.base)
   else: assert false
 
@@ -218,7 +220,7 @@ proc `[]=`*(x: Any, i: int, y: Any) =
   of tyArray:
     var bs = x.rawType.base.size
     if i >=% x.rawType.size div bs:
-      raise newException(IndexError, "index out of bounds")
+      raise newException(IndexError, formatErrorIndexBound(i, x.rawType.size div bs))
     assert y.rawType == x.rawType.base
     genericAssign(x.value +!! i*bs, y.value, y.rawType)
   of tySequence:
@@ -226,7 +228,7 @@ proc `[]=`*(x: Any, i: int, y: Any) =
     if s == nil: raise newException(ValueError, "sequence is nil")
     var bs = x.rawType.base.size
     if i >=% cast[PGenSeq](s).len:
-      raise newException(IndexError, "index out of bounds")
+      raise newException(IndexError, formatErrorIndexBound(i, cast[PGenSeq](s).len-1))
     assert y.rawType == x.rawType.base
     genericAssign(s +!! (GenericSeqSize+i*bs), y.value, y.rawType)
   else: assert false