Skip to content

Commit

Permalink
fix: check if for iterator does not return values
Browse files Browse the repository at this point in the history
Fixes #736.
  • Loading branch information
hishamhm committed Feb 3, 2024
1 parent 541b150 commit 2c6ebe3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/statement/forin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ describe("forin", function()
}))

describe("regression tests", function()
it("catches if iterator function does not return values (#736)", util.check_type_error([[
local function f()
end
for k, v in f() do
end
]], {
{ y = 4, msg = "expression in 'for' statement does not return any values" },
}))

it("does not accept annotations (#701)", util.check_syntax_error([[
for k <const>, v <const> in pairs(table as {string:any}) do
k = "hello"
Expand Down
8 changes: 7 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10795,8 +10795,14 @@ self:expand_type(node, values, elements) })
assert(exptuple.typename == "tuple")
local exptypes = exptuple.tuple

self:widen_all_unions(node)
local exp1 = node.exps[1]
if #exptypes < 1 then
self.errs:invalid_at(exp1, "expression in 'for' statement does not return any values")
return
end

self:widen_all_unions(node)

local args = a_type(node.exps, "tuple", { tuple = {
node.exps[2] and exptypes[2],
node.exps[3] and exptypes[3],
Expand Down
8 changes: 7 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -10795,8 +10795,14 @@ do
assert(exptuple is TupleType)
local exptypes = exptuple.tuple

self:widen_all_unions(node)
local exp1 = node.exps[1]
if #exptypes < 1 then
self.errs:invalid_at(exp1, "expression in 'for' statement does not return any values")
return
end

self:widen_all_unions(node)

local args = a_tuple(node.exps, {
node.exps[2] and exptypes[2],
node.exps[3] and exptypes[3]
Expand Down

0 comments on commit 2c6ebe3

Please sign in to comment.