Skip to content

Commit

Permalink
fixes #18643 [backport:1.0] (#18678)
Browse files Browse the repository at this point in the history
(cherry picked from commit 018465a)
  • Loading branch information
Araq authored and narimiran committed Aug 24, 2021
1 parent e5a3f30 commit f40c941
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ proc genArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) =
if optBoundsCheck in p.options and ty.kind != tyUncheckedArray:
if not isConstExpr(y):
# semantic pass has already checked for const index expressions
if firstOrd(p.config, ty) == 0:
if firstOrd(p.config, ty) == 0 and lastOrd(p.config, ty) >= 0:
if (firstOrd(p.config, b.t) < firstOrd(p.config, ty)) or (lastOrd(p.config, b.t) > lastOrd(p.config, ty)):
linefmt(p, cpsStmts, "if ((NU)($1) > (NU)($2)) #raiseIndexError2($1, $2);$n",
[rdCharLoc(b), intLiteral(lastOrd(p.config, ty))])
Expand Down
11 changes: 11 additions & 0 deletions tests/array/tarray.nim
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,14 @@ block t12466:
a[0'u16 + i] = i
for i in 0'u16 ..< 8'u16:
a[0'u16 + i] = i

block t18643:
# https://github.com/nim-lang/Nim/issues/18643
let a: array[0, int] = []
var caught = false
let b = 9999999
try:
echo a[b]
except IndexDefect:
caught = true
doAssert caught, "IndexDefect not caught!"

0 comments on commit f40c941

Please sign in to comment.