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
parent a77360da5b
commit cc7ec5a6a4

View File

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