From 651c0e45da83a4d389f3fe8246e3835e400c47c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nihlg=C3=A5rd?= Date: Fri, 6 Apr 2018 17:05:28 +0200 Subject: [PATCH] semcheck negative array length (#7518) --- compiler/semtypes.nim | 3 +++ tests/array/tarray.nim | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index b43a96a87..bdc8a3024 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -252,6 +252,9 @@ proc semArrayIndex(c: PContext, n: PNode): PType = if e.typ.kind == tyFromExpr: result = makeRangeWithStaticExpr(c, e.typ.n) 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) elif e.kind == nkSym and e.typ.kind == tyStatic: if e.sym.ast != nil: diff --git a/tests/array/tarray.nim b/tests/array/tarray.nim index 95d1bb7cc..5e947e745 100644 --- a/tests/array/tarray.nim +++ b/tests/array/tarray.nim @@ -46,3 +46,7 @@ let arr3: array[0, string] = [] doAssert(arr1.len == 0) doAssert(arr2.len == 0) doAssert(arr3.len == 0) + +# Negative array length is not allowed (#6852) +doAssert(not compiles(block: + var arr: array[-1, int])) \ No newline at end of file