Skip to content

Support trailing comma in closure use list (PHP 8.0) - #8

Merged
TomasVotruba merged 1 commit into
mainfrom
trailing-comma-closure-use
Oct 10, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
trailing-comma-closure-use

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

function () use ($a, $b,) {} has been valid since PHP 8.0, but the PHP 8 parser rejects it with:

syntax error: unexpected ')', expecting T_VARIABLE or '&'

This breaks class-leak's --blink mode on rector-src. The first such file (rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php) aborts the whole run.

Change

  • lexical_vars now accepts possible_comma. The trailing comma is appended to UseSeparatorTkns, the same way unset($a, $b,) handles it, so printing reproduces the source exactly.
  • Conflict counts are unchanged (3 shift/reduce, 7 reduce/reduce).

Generated parser

php8.go was regenerated with goyacc from golang.org/x/tools v0.51.0, then the compile target's seds were applied (yyErrorVerbose = true, // line) and gofmt -s. Every goyacc release from v0.1.12 up emits narrower table types (int16/uint8 instead of int) than the committed file. That's why the generated diff is large: apart from the tables for the new rule, it's type width and int() casts.

Tests

  • TestTrailingCommaInClosureUseList: parses, 2 uses, 2 separator tokens. Fails without the grammar change.
  • TestTrailingCommaInClosureUseListPrintsBack: parses and prints back to the identical source.
  • TestClosureUseListWithoutTrailingCommaStillParses
  • go test ./... passes.

Note: parsePHP8 in pkg/visitor/printer/printer_php8_test.go actually runs the PHP 7.4 parser, so the new round-trip test lives in internal/php8 instead.

Verified end to end: class-leak's Go port built against this branch scans all of rector-src.

🤖 Generated with Claude Code

https://claude.ai/code/session_01BeUiuxXmYgChK2jkSHJYLi

function () use ($a, $b,) {} is valid since PHP 8.0 but failed with
"unexpected ')', expecting T_VARIABLE or '&'". The trailing comma is kept
as an extra UseSeparatorTkns entry, like unset(), so printing round-trips.

php8.go regenerated with goyacc from golang.org/x/tools v0.51.0 plus the
Makefile compile seds; newer goyacc emits narrower table types, hence the
size of the generated diff.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BeUiuxXmYgChK2jkSHJYLi
@TomasVotruba
TomasVotruba merged commit ce98044 into main Oct 10, 2026
5 checks passed
@TomasVotruba
TomasVotruba deleted the trailing-comma-closure-use branch October 10, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants