Skip to content

Commit

Permalink
fixes #18643 [backport:1.0]
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Aug 12, 2021
1 parent c94933a commit e4f0f0b
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 @@ -920,7 +920,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:

This comment has been minimized.

Copy link
@juancarlospaco

juancarlospaco Aug 12, 2021

Collaborator

@Araq
Does this fix works for JavaScript too?.

Can the compiler emit a warning for an array of static size 0 ?,
I mean whats an array of size 0 useful for?.

This comment has been minimized.

Copy link
@Araq

Araq Aug 12, 2021

Author Member

It is supported for openArrays and varargs and it must work. The JS backend was not affected by this bug.

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); $3}$n",
[rdCharLoc(b), intLiteral(lastOrd(p.config, ty)), raiseInstr(p)])
Expand Down
11 changes: 11 additions & 0 deletions tests/array/tarray.nim
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,14 @@ block t17705:
a = int(a)
var b = array[0, int].high
b = int(b)

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 e4f0f0b

Please sign in to comment.