Skip to content

Commit

Permalink
Fix regression for mapIt (#8567)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LemonBoy authored and Araq committed Aug 8, 2018
1 parent af4f442 commit 32b6209
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,16 @@ template mapIt*(s, op: untyped): untyped =
var it{.inject.}: type(items(s));
op))
var result: seq[outType]
evalOnce(t, s)
when compiles(t.len):
when compiles(s.len):
evalOnce(t, s)
var i = 0
result = newSeq[outType](t.len)
for it {.inject.} in t:
result[i] = op
i += 1
else:
result = @[]
for it {.inject.} in t:
for it {.inject.} in s:
result.add(op)
result

Expand Down Expand Up @@ -1071,5 +1071,10 @@ when isMainModule:
proc foo(x: openArray[int]): seq[int] = x.mapIt(it + 1)
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):
echo "Finished doc tests"

0 comments on commit 32b6209

Please sign in to comment.