Skip to content

Commit

Permalink
fix: for-in variables don't accept annotations
Browse files Browse the repository at this point in the history
Closes #701.
  • Loading branch information
hishamhm committed Oct 30, 2023
1 parent 810a95e commit f7a2f41
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions spec/statement/forin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ describe("forin", function()
}))

describe("regression tests", function()
it("does not accept annotations (#701)", util.check_syntax_error([[
for k <const>, v <const> in pairs(table as {string:any}) do
k = "hello"
print(k, v)
end
]], {
-- skips one bad token at a time. Too much of a corner case to
-- add parser code just to produce a nicer error, the generic
-- message should be enough to indicate this is not supporter.
{ y = 1, msg = "syntax error" },
{ y = 1, msg = "syntax error" },
{ y = 1, msg = "syntax error" },
{ y = 1, msg = "syntax error" },
{ y = 1, msg = "syntax error" },
{ y = 1, msg = "syntax error" },
}))

it("with an iterator declared as a nominal (#183)", util.check([[
local type Iterator = function(): string
Expand Down
2 changes: 1 addition & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ local function parse_forin(ps, i)
local node = new_node(ps.tokens, i, "forin")
i = i + 1
node.vars = new_node(ps.tokens, i, "variable_list")
i, node.vars = parse_list(ps, i, node.vars, { ["in"] = true }, "sep", parse_variable_name)
i, node.vars = parse_list(ps, i, node.vars, { ["in"] = true }, "sep", parse_identifier)
i = verify_tk(ps, i, "in")
node.exps = new_node(ps.tokens, i, "expression_list")
i = parse_list(ps, i, node.exps, { ["do"] = true }, "sep", parse_expression)
Expand Down
4 changes: 2 additions & 2 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,7 @@ parse_argument_type_list = function(ps: ParseState, i: integer): integer, Type
return i, list
end

local function parse_identifier(ps: ParseState, i: integer): integer, Node
local function parse_identifier(ps: ParseState, i: integer): integer, Node, integer
if ps.tokens[i].kind == "identifier" then
return i + 1, new_node(ps.tokens, i, "identifier")
end
Expand Down Expand Up @@ -2532,7 +2532,7 @@ local function parse_forin(ps: ParseState, i: integer): integer, Node
local node = new_node(ps.tokens, i, "forin")
i = i + 1
node.vars = new_node(ps.tokens, i, "variable_list")
i, node.vars = parse_list(ps, i, node.vars, { ["in"] = true }, "sep", parse_variable_name)
i, node.vars = parse_list(ps, i, node.vars, { ["in"] = true }, "sep", parse_identifier)
i = verify_tk(ps, i, "in")
node.exps = new_node(ps.tokens, i, "expression_list")
i = parse_list(ps, i, node.exps, { ["do"] = true }, "sep", parse_expression)
Expand Down

0 comments on commit f7a2f41

Please sign in to comment.