Skip to content

fix: allow match expressions as cast and clone operands - #309

Merged
calebdw merged 6 commits into
tree-sitter:masterfrom
akashchamp:fix-match-cast-clone-operand
Nov 19, 2026
Merged

calebdw merged 6 commits into
tree-sitter:masterfrom
akashchamp:fix-match-cast-clone-operand

Conversation

@akashchamp

@akashchamp akashchamp commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

(int)match($x) { ... } and clone match($x) { ... } are valid PHP but parsed as errors, because cast_expression and clone_expression did not accept a match_expression operand. This adds it to both.

Fixes #308. I left (int)yield (also listed there) out, since it is a separate construct.

Checked with tree-sitter test (149/149 parses pass) and npm run lint. The new corpus test fails on the old parser and passes on the new one. Parser files are rebuilt by tree-sitter 0.27.1 in a separate chore: generate commit.

akashchamp and others added 3 commits November 19, 2026 09:35
Preserve regression coverage for casts, bare clone, and function-style
clone arguments containing match expressions. The general operand fix
provides the implementation.

Refs tree-sitter#308.
Accept cast keywords as expression names so argument values can continue
into scoped calls and compound expressions. Keep class-name aliases
limited to cast spellings that PHP permits as class identifiers.

Accept type keywords in valid global and class constant declarations,
with coverage for reserved names, typed constants, casts, and named
arguments. Restore example parsing for the affected WordPress file.

Fixes tree-sitter#295.
Use expression operands and PHP precedence instead of operand allowlists
so match, yield, nested clones, and exponentiation parse consistently.
Allow general instanceof left operands so casts retain their precedence.

Remove the cast-variable workaround: casts apply to assignment results,
not assignment targets. Align unary and error-suppression precedence
with casts and exponentiation.

Cover assignments, coalescing, conditionals, includes, print, throw,
yields, nested clones, and unary operators with PHP-validated cases.

Fixes tree-sitter#308.
@calebdw
calebdw force-pushed the fix-match-cast-clone-operand branch from 93609cb to 63f4ceb Compare November 19, 2026 15:57
PHPUnit added a name-resolution fixture containing an intentionally
invalid assignment. Expect its parse failure so example checks reject
real regressions without failing on this upstream negative test.
Regenerate both PHP parsers with tree-sitter 0.27.2 for keyword identifiers
and general cast and clone operands with PHP operator precedence.
Prepare the keyword identifier and constant parsing fixes for release.
Keep package manifests, lockfiles, and parser metadata in sync.
@calebdw
calebdw force-pushed the fix-match-cast-clone-operand branch from 63f4ceb to 6ecd578 Compare November 19, 2026 16:04
@calebdw
calebdw merged commit 75f0fcb into tree-sitter:master Nov 19, 2026
3 of 4 checks passed
@calebdw

calebdw commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks, @akashchamp, for identifying this and contributing the fix and regression test! Your report led us to look more closely at how we handle cast and clone operands.

We kept the match regression tests, but replaced the two explicit match_expression additions with a general fix. PHP’s own grammar accepts an expression after a cast or clone; precedence determines how much of that expression belongs to the operand. Our narrower operand lists were also missing valid cases such as casts of yield/yield from and nested clones.

The merged implementation uses general expression operands with explicit precedence. We also removed the cast-variable workaround, corrected casts around assignment results, and aligned the related instanceof, unary, and error-suppression precedence rules. This avoids continuing to add individual expression types to operand allowlists.

Your original contribution is preserved as a test-only commit with your author credit. We added coverage for function-style clone(match(...), ...), named arguments, yields, nested clones, exponentiation, assignments, conditionals, and other expression operands. We checked the relevant syntax and evaluation behavior against PHP 8.5, and both grammars generate without new conflict declarations.

This PR also incorporated the keyword identifier/constant fixes from #307. Generated parser updates remain in one separate commit, followed by the version bump. The example-parsing CI failure turned out to be a newly added, intentionally invalid PHPUnit fixture, which we added to the expected-invalid list.

Thanks again for getting this started!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cast and clone operands reject match expressions: (int)match($x) {...} and clone match($x) {...} are parse errors

2 participants