fix(parser): allow comments between a bare return and its terminator - #418
Merged
Merged
Conversation
A valueless `return` followed by a comment before the block terminator (`elseif`/`else`/`end`/`until`) failed to parse with "Expected expression". `parse_return/1` peeked at the raw next token to detect an empty return; a comment token is not a terminator, so it fell through to `parse_expr_list` and choked on the comment. Comments are whitespace in Lua, so peek past them (`skip_comments/1`) when deciding whether the return is bare. The comment tokens are left in the stream so the block parser still collects them as orphaned/trailing comments. Real Lua 5.3 and Luerl both accept this; the idiom shows up in `if ... then return -- note \n elseif ...` style branches. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
davydog187
approved these changes
Jul 28, 2026
Contributor
|
Thanks! 🍍 🍍 🍍 🍍 🍍 |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A valueless
returnfollowed by a comment before the block terminator (elseif/else/end/until) fails to parse:Comments are whitespace in Lua, so the
returnhere is simply a bare (valueless) return and the branch continues normally. Reference Lua 5.3 accepts this, and it's a common idiom (then return -- note \n elseif ...).Root cause
Lua.Parser.parse_return/1peeked at the raw next token to decide whether thereturnwas valueless. A comment token is not one of the terminators (end/else/elseif/until), nor EOF/;, so it fell through toparse_expr_list, which then choked on the comment → "Expected expression".Fix
Peek past comment tokens (via the existing
skip_comments/1) when detecting the terminator. The comment tokens are left in the stream so the block parser still collects them as orphaned/trailing comments — no comment metadata is lost.The semicolon branch consumes past comments as well, so
return -- notefollowed by;is handled too.Tests
Added a regression test in
test/lua/parser/statement_test.exscovering a barereturnwith a comment beforeelseif/else/end— fails before, passes after.mix test test/lua/parser/— all greenmix test(full suite) — all green🤖 Generated with Claude Code