summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-09-29 15:38:51 +0800
committerGitHub <noreply@github.com>2023-09-29 09:38:51 +0200
commita8d55fdec7e6e534546f9d6c116d9a76393c534b (patch)
tree34f10014c993b1734f2fd67b7e126fb2ed36c5d1
parent8761599aade64f5953ef7d3c4cea005a0c459355 (diff)
downloadNim-a8d55fdec7e6e534546f9d6c116d9a76393c534b.tar.gz
deprecates `newSeqUninitialized` replaced by `newSeqUninit` (#22739)
ref #19727
closes #22586

https://github.com/nim-lang/Nim/issues/22554 needs it to move on.
`newSeqUnsafe` can be introduced later.
-rw-r--r--changelog.md1
-rw-r--r--lib/system.nim31
-rw-r--r--lib/system/seqs_v2.nim2
-rw-r--r--testament/important_packages.nim4
4 files changed, 32 insertions, 6 deletions
diff --git a/changelog.md b/changelog.md
index 795386144..74020e8fa 100644
--- a/changelog.md
+++ b/changelog.md
@@ -19,6 +19,7 @@ slots when enlarging a sequence.
 
 [//]: # "Deprecations:"
 
+- Deprecates `system.newSeqUninitialized`, which is replaced by `newSeqUninit`.
 
 [//]: # "Removals:"
 
diff --git a/lib/system.nim b/lib/system.nim
index 8e16d17d4..b9dd7f143 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -627,7 +627,7 @@ proc newSeq*[T](len = 0.Natural): seq[T] =
   ##
   ## See also:
   ## * `newSeqOfCap <#newSeqOfCap,Natural>`_
-  ## * `newSeqUninitialized <#newSeqUninitialized,Natural>`_
+  ## * `newSeqUninit <#newSeqUninit,Natural>`_
   newSeq(result, len)
 
 proc newSeqOfCap*[T](cap: Natural): seq[T] {.
@@ -1606,12 +1606,22 @@ when not defined(js) and defined(nimV2):
       flags: int
     PNimTypeV2 = ptr TNimTypeV2
 
+proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
+
 when notJSnotNims and defined(nimSeqsV2):
   include "system/strs_v2"
   include "system/seqs_v2"
 
 when not defined(js):
-  proc newSeqUninitialized*[T: SomeNumber](len: Natural): seq[T] =
+  template newSeqImpl(T, len) =
+    result = newSeqOfCap[T](len)
+    when defined(nimSeqsV2):
+      cast[ptr int](addr result)[] = len
+    else:
+      var s = cast[PGenericSeq](result)
+      s.len = len
+
+  proc newSeqUninitialized*[T: SomeNumber](len: Natural): seq[T] {.deprecated: "Use `newSeqUninit` instead".} =
     ## Creates a new sequence of type `seq[T]` with length `len`.
     ##
     ## Only available for numbers types. Note that the sequence will be
@@ -1630,6 +1640,23 @@ when not defined(js):
       var s = cast[PGenericSeq](result)
       s.len = len
 
+  proc newSeqUninit*[T](len: Natural): seq[T] =
+    ## Creates a new sequence of type `seq[T]` with length `len`.
+    ##
+    ## Only available for types, which don't contain
+    ## managed memory or have destructors.
+    ## Note that the sequence will be uninitialized.
+    ## After the creation of the sequence you should assign
+    ## entries to the sequence instead of adding them.
+    runnableExamples:
+      var x = newSeqUninit[int](3)
+      assert len(x) == 3
+      x[0] = 10
+    when supportsCopyMem(T):
+      newSeqImpl(T, len)
+    else:
+      {.error: "The type T cannot contain managed memory or have destructors".}
+
   proc newStringUninit*(len: Natural): string =
     ## Returns a new string of length `len` but with uninitialized
     ## content. One needs to fill the string character after character
diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim
index 7ce8de054..ee8f2d67e 100644
--- a/lib/system/seqs_v2.nim
+++ b/lib/system/seqs_v2.nim
@@ -16,8 +16,6 @@
 {.push warning[StrictNotNil]: off.}  # See https://github.com/nim-lang/Nim/issues/21401
 
 
-proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
-
 ## Default seq implementation used by Nim's core.
 type
   NimSeqPayloadBase = object
diff --git a/testament/important_packages.nim b/testament/important_packages.nim
index 462bf8a30..f769c4ebd 100644
--- a/testament/important_packages.nim
+++ b/testament/important_packages.nim
@@ -35,7 +35,7 @@ proc pkg(name: string; cmd = "nimble test"; url = "", useHead = true, allowFailu
 
 pkg "alea"
 pkg "argparse"
-pkg "arraymancer", "nim c tests/tests_cpu.nim"
+pkg "arraymancer", "nim c tests/tests_cpu.nim", url = "https://github.com/nim-lang/Arraymancer"
 pkg "ast_pattern_matching", "nim c -r tests/test1.nim"
 pkg "asyncftpclient", "nimble compileExample"
 pkg "asyncthreadpool", "nimble test --mm:refc"
@@ -96,7 +96,7 @@ pkg "measuremancer", "nimble testDeps; nimble -y test"
 pkg "memo"
 pkg "msgpack4nim", "nim c -r tests/test_spec.nim"
 pkg "nake", "nim c nakefile.nim"
-pkg "neo", "nim c -d:blas=openblas --mm:refc tests/all.nim"
+pkg "neo", "nim c -d:blas=openblas --mm:refc tests/all.nim", url = "https://github.com/nim-lang/neo"
 pkg "nesm", "nimble tests", "https://github.com/nim-lang/NESM", useHead = true
 pkg "netty"
 pkg "nico", allowFailure = true