Skip to content

Commit c4c3843

Browse files
committed
don't crash when non-value is at end of loop expression
1 parent ef4157f commit c4c3843

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

moonscript/transform.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,19 @@ Accumulator = (function()
12501250
end
12511251
else
12521252
body = apply_to_last(body, function(n)
1253-
return build.assign_one(self.value_name, n)
1253+
if types.is_value(n) then
1254+
return build.assign_one(self.value_name, n)
1255+
else
1256+
return build.group({
1257+
{
1258+
"declare",
1259+
{
1260+
self.value_name
1261+
}
1262+
},
1263+
n
1264+
})
1265+
end
12541266
end)
12551267
val = self.value_name
12561268
end

moonscript/transform.moon

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,14 @@ class Accumulator
651651
body = {}
652652
else
653653
body = apply_to_last body, (n) ->
654-
build.assign_one @value_name, n
654+
if types.is_value n
655+
build.assign_one @value_name, n
656+
else
657+
-- just ignore it
658+
build.group {
659+
{"declare", {@value_name}}
660+
n
661+
}
655662
@value_name
656663

657664
update = {

tests/inputs/loops.moon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ i = 0
7474
x = while i < 10
7575
i += 1
7676

77+
-- values that can'e be coerced
78+
79+
x = for thing in *3
80+
y = "hello"
81+
82+
x = for x=1,2
83+
y = "hello"
84+
85+
7786
-- continue
7887

7988
while true

tests/outputs/loops.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ x = (function()
143143
end
144144
return _accum_0
145145
end)()
146+
x = (function()
147+
local _accum_0 = { }
148+
local _len_0 = 0
149+
local _list_1 = 3
150+
for _index_0 = 1, #_list_1 do
151+
local thing = _list_1[_index_0]
152+
local _value_0
153+
y = "hello"
154+
if _value_0 ~= nil then
155+
_len_0 = _len_0 + 1
156+
_accum_0[_len_0] = _value_0
157+
end
158+
end
159+
return _accum_0
160+
end)()
161+
x = (function()
162+
local _accum_0 = { }
163+
local _len_0 = 0
164+
for x = 1, 2 do
165+
local _value_0
166+
y = "hello"
167+
if _value_0 ~= nil then
168+
_len_0 = _len_0 + 1
169+
_accum_0[_len_0] = _value_0
170+
end
171+
end
172+
return _accum_0
173+
end)()
146174
while true do
147175
local _continue_0 = false
148176
repeat

0 commit comments

Comments
 (0)