summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-10-16 20:09:00 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-10-16 20:09:12 +0200
commit0d48bafcf08bb2be72029ccebc745b5594d49731 (patch)
treef71dd6334a98e732a37f9b047b5f5b0430ed7caf
parentb340f677ebf0fe4224163e0735f16b4acb9fbed8 (diff)
downloadNim-0d48bafcf08bb2be72029ccebc745b5594d49731.tar.gz
fixes a regression about indexing into UncheckedArray
-rw-r--r--compiler/semexprs.nim13
-rw-r--r--tests/array/tarray.nim4
2 files changed, 16 insertions, 1 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 78ea82bb9..b811170f7 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -1358,7 +1358,7 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode =
 
   case arr.kind
   of tyArray, tyOpenArray, tyVarargs, tySequence, tyString,
-     tyCString, tyUncheckedArray:
+     tyCString:
     if n.len != 2: return nil
     n.sons[0] = makeDeref(n.sons[0])
     for i in countup(1, sonsLen(n) - 1):
@@ -1371,6 +1371,17 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode =
       result = n
       result.typ = elemType(arr)
     #GlobalError(n.info, errIndexTypesDoNotMatch)
+  of tyUncheckedArray:
+    if n.len != 2: return nil
+    n.sons[0] = makeDeref(n.sons[0])
+    for i in countup(1, sonsLen(n) - 1):
+      n.sons[i] = semExprWithType(c, n.sons[i],
+                                  flags*{efInTypeof, efDetermineType})
+    # index into unchecked array must be some integer type:
+    if n.sons[1].typ.skipTypes(abstractRange-{tyDistinct}).kind in
+        {tyInt..tyInt64, tyUInt..tyUInt64}:
+      result = n
+      result.typ = elemType(arr)
   of tyTypeDesc:
     # The result so far is a tyTypeDesc bound
     # a tyGenericBody. The line below will substitute
diff --git a/tests/array/tarray.nim b/tests/array/tarray.nim
index 948e63a89..e35a804ee 100644
--- a/tests/array/tarray.nim
+++ b/tests/array/tarray.nim
@@ -532,3 +532,7 @@ block t7818:
 
     doAssert(testOpenArray(@[u.addr, v.addr, w.addr]) == "123")
     doAssert(testOpenArray(@[w.addr, u.addr, v.addr]) == "312")
+
+# regression regarding unchecked array indexing:
+proc foo(x: ptr UncheckedArray[int]; idx: uint64) =
+  echo x[idx]