Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax error when return; is not the last statement of its block #893

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/lang/parser/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ describe("parser", function()
assert.same("return", result.ast[1].kind)
end)

it("does not accept statements after 'return;' (regression test for #495)", function ()
local result = tl.process_string([[
return;
print("")
]])
assert.same(1, #result.syntax_errors)
assert.same(result.syntax_errors[1].msg, "return must be the last statement of its block")

local result = tl.process_string([[
local function get_foo(): string
if true then
return "foo";
print("")
end
end
]])
assert.same(1, #result.syntax_errors)
assert.same(result.syntax_errors[1].msg, "return must be the last statement of its block")
end)

it("accepts semicolons in tables (regression test for #54)", function ()
local result = tl.process_string([[
local t = {
Expand Down
3 changes: 3 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,9 @@ local function parse_return(ps: ParseState, i: integer): integer, Node
i = parse_list(ps, i, node.exps, stop_return_list, "sep", parse_expression)
if ps.tokens[i].kind == ";" then
i = i + 1
if ps.tokens[i].kind ~= "$EOF$" and not stop_statement_list[ps.tokens[i].kind] then
return fail(ps, i, "return must be the last statement of its block")
end
end
return i, node
end
Expand Down
Loading