semcheck negative array length (#7518)
This commit is contained in:
parent
7e1f0e28ae
commit
651c0e45da
2 changed files with 7 additions and 0 deletions
|
|
@ -252,6 +252,9 @@ proc semArrayIndex(c: PContext, n: PNode): PType =
|
||||||
if e.typ.kind == tyFromExpr:
|
if e.typ.kind == tyFromExpr:
|
||||||
result = makeRangeWithStaticExpr(c, e.typ.n)
|
result = makeRangeWithStaticExpr(c, e.typ.n)
|
||||||
elif e.kind in {nkIntLit..nkUInt64Lit}:
|
elif e.kind in {nkIntLit..nkUInt64Lit}:
|
||||||
|
if e.intVal < 0:
|
||||||
|
localError(n[1].info,
|
||||||
|
"Array length can't be negative, but was " & $e.intVal)
|
||||||
result = makeRangeType(c, 0, e.intVal-1, n.info, e.typ)
|
result = makeRangeType(c, 0, e.intVal-1, n.info, e.typ)
|
||||||
elif e.kind == nkSym and e.typ.kind == tyStatic:
|
elif e.kind == nkSym and e.typ.kind == tyStatic:
|
||||||
if e.sym.ast != nil:
|
if e.sym.ast != nil:
|
||||||
|
|
|
||||||
|
|
@ -46,3 +46,7 @@ let arr3: array[0, string] = []
|
||||||
doAssert(arr1.len == 0)
|
doAssert(arr1.len == 0)
|
||||||
doAssert(arr2.len == 0)
|
doAssert(arr2.len == 0)
|
||||||
doAssert(arr3.len == 0)
|
doAssert(arr3.len == 0)
|
||||||
|
|
||||||
|
# Negative array length is not allowed (#6852)
|
||||||
|
doAssert(not compiles(block:
|
||||||
|
var arr: array[-1, int]))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue