diff options
author | Eric N. Vander Weele <ericvw@gmail.com> | 2023-03-20 13:51:31 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-20 18:51:31 +0100 |
commit | da7833c68bd8a3fea4b380e2a0e84753812450fe (patch) | |
tree | 653b80af266c62f0e57e4659b018c8772df66857 | |
parent | 285ea3c48e7b01fe6beecf794e9e8cc904c27889 (diff) | |
download | Nim-da7833c68bd8a3fea4b380e2a0e84753812450fe.tar.gz |
fixes #21538; expand len template parameter once in newSeqWith (#21543)
`len` could contain side effects and may result in different values when substituted twice in the template expansion. Instead, capture the result of substituting `len` once. closes: #21538
-rw-r--r-- | lib/pure/collections/sequtils.nim | 6 | ||||
-rw-r--r-- | tests/stdlib/tsequtils.nim | 5 |
2 files changed, 8 insertions, 3 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 1773e827b..bcdd0879d 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -1077,9 +1077,9 @@ template newSeqWith*(len: int, init: untyped): untyped = 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: + let newLen = len + var result = newSeq[typeof(init)](newLen) + for i in 0 ..< newLen: result[i] = init move(result) # refs bug #7295 diff --git a/tests/stdlib/tsequtils.nim b/tests/stdlib/tsequtils.nim index 176c00214..2b9ef5d6e 100644 --- a/tests/stdlib/tsequtils.nim +++ b/tests/stdlib/tsequtils.nim @@ -388,6 +388,11 @@ block: # newSeqWith tests seq2D[0][1] = true doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]] +block: # bug #21538 + var x: seq[int] = @[2, 4] + var y = newSeqWith(x.pop(), true) + doAssert y == @[true, true, true, true] + block: # mapLiterals tests let x = mapLiterals([0.1, 1.2, 2.3, 3.4], int) doAssert x is array[4, int] |