Fix #3778: Make for loops more consistent

The following two lines might seem equivalent:

    for n in [1, 2, 3] by  a then a = 4; n
    for n in [1, 2, 3] by +a then a = 4; n

But they used not to be, because `+a` was cached into a `ref`, while the plain
`a` wasn’t. Now even simple identifiers are cached, making the two lines
equivalent as expected.
This commit is contained in:
Simon Lydell
2015-01-11 21:19:24 +01:00
parent 934bd2acc7
commit 996a171a4e
3 changed files with 48 additions and 25 deletions

View File

@@ -555,3 +555,14 @@ test "splats in destructuring in comprehensions", ->
test "#156: expansion in destructuring in comprehensions", ->
list = [[0, 1, 2], [2, 3, 4], [4, 5, 6]]
arrayEq (last for [..., last] in list), [2, 4, 6]
test "#3778: Consistently always cache for loop range boundaries and steps, even
if they are simple identifiers", ->
a = 1; arrayEq [1, 2, 3], (for n in [1, 2, 3] by a then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [1, 2, 3] by +a then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [a..3] then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [+a..3] then a = 4; n)
a = 3; arrayEq [1, 2, 3], (for n in [1..a] then a = 4; n)
a = 3; arrayEq [1, 2, 3], (for n in [1..+a] then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by a then a = 4; n)
a = 1; arrayEq [1, 2, 3], (for n in [1..3] by +a then a = 4; n)