summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorOscar NihlgÄrd <oscarnihlgard@gmail.com>2018-04-06 17:05:28 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-06 17:05:28 +0200
commit651c0e45da83a4d389f3fe8246e3835e400c47c4 (patch)
tree226d0a52b7ba6f8d2a2ec7939478b88b8157e644
parent7e1f0e28aeb2b699fe93883fc6743ad9ac7a4354 (diff)
downloadNim-651c0e45da83a4d389f3fe8246e3835e400c47c4.tar.gz
semcheck negative array length (#7518)
-rw-r--r--compiler/semtypes.nim3
-rw-r--r--tests/array/tarray.nim4
2 files changed, 7 insertions, 0 deletions
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