Fix regression for mapIt (#8567)

Don't try to be too smart and limit the use of `evalOnce` where strictly
needed as not every value can be assigned with a `let`.

Fixes #8566
This commit is contained in:
LemonBoy 2018-08-08 15:34:21 +02:00 • committed by Andreas Rumpf
commit 32b62097a2

View file

@ -676,8 +676,8 @@ template mapIt*(s, op: untyped): untyped =
var it{.inject.}: type(items(s)); var it{.inject.}: type(items(s));
op)) op))
var result: seq[outType] var result: seq[outType]
evalOnce(t, s) when compiles(s.len):
when compiles(t.len): evalOnce(t, s)
var i = 0 var i = 0
result = newSeq[outType](t.len) result = newSeq[outType](t.len)
for it {.inject.} in t: for it {.inject.} in t:
@ -685,7 +685,7 @@ template mapIt*(s, op: untyped): untyped =
i += 1 i += 1
else: else:
result = @[] result = @[]
for it {.inject.} in t: for it {.inject.} in s:
result.add(op) result.add(op)
result result
@ -1071,5 +1071,10 @@ when isMainModule:
proc foo(x: openArray[int]): seq[int] = x.mapIt(it + 1) proc foo(x: openArray[int]): seq[int] = x.mapIt(it + 1)
doAssert foo([1,2,3]) == @[2,3,4] doAssert foo([1,2,3]) == @[2,3,4]
block: # mapIt with invalid RHS for `let` (#8566)
type X = enum
A, B
doAssert mapIt(X, $it) == @["A", "B"]
when not defined(testing): when not defined(testing):
echo "Finished doc tests" echo "Finished doc tests"