fix #7295: use move(result) inside template to avoid copy with --gc:refc (#18168)

This commit is contained in:
Timothee Cour 2021-06-04 15:32:46 -07:00 • committed by GitHub
commit cc7ec5a6a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1014,27 +1014,27 @@ template applyIt*(varSeq, op: untyped) =
template newSeqWith*(len: int, init: untyped): untyped = template newSeqWith*(len: int, init: untyped): untyped =
## Creates a new sequence of length `len`, calling `init` to initialize ## Creates a new `seq` of length `len`, calling `init` to initialize
## each value of the sequence. ## each value of the seq.
##
## Useful for creating "2D" sequences - sequences containing other sequences
## or to populate fields of the created sequence.
## ##
## Useful for creating "2D" seqs - seqs containing other seqs
## or to populate fields of the created seq.
runnableExamples: runnableExamples:
## Creates a sequence containing 5 bool sequences, each of length of 3. ## Creates a seq containing 5 bool seqs, each of length of 3.
var seq2D = newSeqWith(5, newSeq[bool](3)) var seq2D = newSeqWith(5, newSeq[bool](3))
assert seq2D.len == 5 assert seq2D.len == 5
assert seq2D[0].len == 3 assert seq2D[0].len == 3
assert seq2D[4][2] == false assert seq2D[4][2] == false
## Creates a sequence of 20 random numbers from 1 to 10 ## Creates a seq with random numbers
import random import std/random
var seqRand = newSeqWith(20, rand(10)) var seqRand = newSeqWith(20, rand(1.0))
assert seqRand[0] != seqRand[1]
var result = newSeq[typeof(init)](len) var result = newSeq[typeof(init)](len)
for i in 0 ..< len: for i in 0 ..< len:
result[i] = init result[i] = init
result move(result) # refs bug #7295
func mapLitsImpl(constructor: NimNode; op: NimNode; nested: bool; func mapLitsImpl(constructor: NimNode; op: NimNode; nested: bool;
filter = nnkLiterals): NimNode = filter = nnkLiterals): NimNode =