From 3ff4949e4762f46f588d233dd98da3dd289fd5e0 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 8 Aug 2018 22:45:53 -0700 Subject: [PATCH] fix #7187 toSeq twice; adds test case --- lib/pure/collections/sequtils.nim | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index eb08fdcb08b72..e04adc817c466 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -513,9 +513,10 @@ template toSeq*(iter: untyped): untyped = ## assert odd_numbers == @[1, 3, 5, 7, 9] when compiles(iter.len): + evalOnce(t_D20180808T230638, iter, letAssigneable = true) var i = 0 - var result = newSeq[type(iter)](iter.len) - for x in iter: + var result = newSeq[type(iter)](t_D20180808T230638.len) + for x in t_D20180808T230638: result[i] = x inc i result @@ -1012,6 +1013,16 @@ when isMainModule: result = true) assert odd_numbers == @[1, 3, 5, 7, 9] + block: + # tests https://github.com/nim-lang/Nim/issues/7187 + var counter = 0 + proc getInput():auto = + counter.inc + @[1, 2, 3] + let ret = toSeq(getInput().filter(proc (x: int): bool = x < 3)) + doAssert ret == @[1, 2] + doAssert counter == 1 + block: # foldl tests let numbers = @[5, 9, 11] @@ -1068,13 +1079,13 @@ when isMainModule: doAssert openArray[int]([1,2]).mapIt(it) == @[1,2] block: + # tests https://github.com/nim-lang/Nim/issues/7187: + # make sure argument evaluated only once, analog to var counter = 0 proc getInput():auto = counter.inc [1, 2] doAssert getInput().mapIt(it*2).mapIt(it*10) == @[20, 40] - # make sure argument evaluated only once, analog to - # https://github.com/nim-lang/Nim/issues/7187 test case doAssert counter == 1 block: # mapIt with invalid RHS for `let` (#8566)