* 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`
This commit is contained in:
Timothee Cour 2019-02-10 13:07:11 -08:00 • committed by Andreas Rumpf
commit 4910608394
11 changed files with 88 additions and 36 deletions

View file

@ -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