From 2c6ebe312b2374b7b070401fcceb0488ac390b59 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 26 Jan 2024 17:50:18 -0300 Subject: [PATCH] fix: check if for iterator does not return values Fixes #736. --- spec/statement/forin_spec.lua | 10 ++++++++++ tl.lua | 8 +++++++- tl.tl | 8 +++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spec/statement/forin_spec.lua b/spec/statement/forin_spec.lua index 9f93f131b..c473b1c55 100644 --- a/spec/statement/forin_spec.lua +++ b/spec/statement/forin_spec.lua @@ -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 , v in pairs(table as {string:any}) do k = "hello" diff --git a/tl.lua b/tl.lua index 053e6edde..5541e8ee2 100644 --- a/tl.lua +++ b/tl.lua @@ -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], diff --git a/tl.tl b/tl.tl index d6dd29776..a021db2f3 100644 --- a/tl.tl +++ b/tl.tl @@ -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]