summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorc-blake <c-blake@users.noreply.github.com>2018-10-12 11:40:07 -0400
committerAndreas Rumpf <rumpf_a@web.de>2018-10-12 17:40:07 +0200
commit1b3725e3954ca01e5f37e82845549bdcce7d3901 (patch)
treee90fc69e6f3574bd716727164860282ec70d3b76
parent14925ee8b6c33ae1477c729b52195d38e4d68b32 (diff)
downloadNim-1b3725e3954ca01e5f37e82845549bdcce7d3901.tar.gz
Add `toOpenArray[T](ptr UncheckedArray[T])` for clarity. (#9316)
* Add `toOpenArray[T](ptr UncheckedArray[T])` for clarity.  `ptr array[0,T]`
for some unchecked type already works but A) `UncheckedArray` seems to be
the intended future way for this kind of access, and B) essentially all use
cases will have a `ptr` for that kind of array source and this call signature
lets callers drop the trailing `[]` corresponding to that `ptr` deref.
This PR relates to issue https://github.com/nim-lang/Nim/issues/9001 .

* Add a test for toOpenArray() for UncheckedArray[T]s.
-rw-r--r--compiler/ccgcalls.nim2
-rw-r--r--lib/system.nim2
-rw-r--r--tests/system/tsystem_misc.nim5
3 files changed, 8 insertions, 1 deletions
diff --git a/compiler/ccgcalls.nim b/compiler/ccgcalls.nim
index 33b07a5a7..b23cd598e 100644
--- a/compiler/ccgcalls.nim
+++ b/compiler/ccgcalls.nim
@@ -106,7 +106,7 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
         result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
       else:
         result = "($1)+(($2)-($4)), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), intLiteral(first)]
-    of tyOpenArray, tyVarargs:
+    of tyOpenArray, tyVarargs, tyUncheckedArray:
       result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
     of tyString, tySequence:
       if skipTypes(n.typ, abstractInst).kind == tyVar and
diff --git a/lib/system.nim b/lib/system.nim
index dff149249..08d4d9411 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -4338,6 +4338,8 @@ when not defined(js):
     magic: "Slice".}
   proc toOpenArray*[T](x: openarray[T]; first, last: int): openarray[T] {.
     magic: "Slice".}
+  proc toOpenArray*[T](x: ptr UncheckedArray[T]; first, last: int): openarray[T] {.
+    magic: "Slice".}
   proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openarray[T] {.
     magic: "Slice".}
   proc toOpenArray*(x: string; first, last: int): openarray[char] {.
diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim
index c8189b3b1..3bbb5eff1 100644
--- a/tests/system/tsystem_misc.nim
+++ b/tests/system/tsystem_misc.nim
@@ -15,6 +15,7 @@ discard """
 1
 2
 3
+2
 48
 49
 50
@@ -98,6 +99,10 @@ doAssertRaises(IndexError):
 doAssertRaises(IndexError):
   foo(toOpenArray(arrNeg, -1, -3))
 
+type seqqType = ptr UncheckedArray[int]
+let qData = cast[seqqType](addr seqq[0])
+oaFirstElm(toOpenArray(qData, 1, 3))
+
 proc foo(a: openArray[byte]) =
   for x in a: echo x