Skip to content

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

Closed

Description

@ishizakahiroshi

Summary

match expressions are rejected as the operand of a cast and of clone. These are valid PHP (8.0+), but the parser reports a syntax error for the whole file:

<?php
function f($u) {
    return (int)match($u) { 'g' => 1, default => 2 };
}

php -l accepts it. The same match is parsed fine after a unary -, !, @, after an assignment, inside parentheses, or on its own.

Versions

  • tree-sitter-php 0.24.1 and 0.25.1 (the Python package; 0.25.1 is the latest release)
  • tree-sitter (Python binding) 0.26.0
  • PHP 8.5.7 php -l: "No syntax errors detected" for every snippet below (the same code runs on PHP 8.4)

Results (each snippet is a one-function file; has_error of the root node)

Code 0.25.1
return (int)match($u){ 1 => 1, default => 2 }; error
return (int) match($u){ ... }; (space after the cast) error
return (float)match($u){ ... }; error
return clone match($u){ 1 => new stdClass, default => new stdClass }; error
$x = (int)yield 1; (inside a generator) error
return (int)(match($u){ ... }); ok
return (int)@match($u){ ... }; ok
return @match($u){ ... }; ok
return -match($u){ ... }; / return !match($u){ ... }; ok
$m = match($u){ ... }; return (int)$m; ok
return match($u){ ... }; ok

In a multi-line version, the tree has a MISSING ; right after (int)match ($u) (the statement ends too early), followed by an ERROR node that starts at the first arm of the match body.

Where it comes from (reading common/define-grammar.js on master; I did not try a fix or regenerate the parser)

  • cast_expression takes field('value', choice($._unary_expression, $.include_expression, $.include_once_expression, $.error_suppression_expression)).
  • match_expression (and yield_expression) are alternatives of expression, but they are not part of _unary_expression or primary_expression.
  • unary_op_expression takes $.expression as its argument, and error_suppression_expression is '@' $.expression, which is why -match, !match, @match and (int)@match work.
  • clone_expression takes $.primary_expression (or arguments), so clone match(...) {...} fails for the same reason.

So the cast (and clone) operand seems to need match_expression (and, for (int)yield, yield_expression).

Expected

No syntax error: (int)match(...) {...} parses as a cast of a match expression, the same way (int)$x or (int)foo() does.

Impact

A static analysis tool built on this grammar has to give up on the whole file when a single such expression exists.

Activity

  1. calebdw commented on Oct 20, 2026

    @calebdw
    Collaborator

    I'll take a look when I get the chance, but feel free to submit PR if you have time

    A static analysis tool built on this grammar has to give up on the whole file when a single such expression exists.

    FYI: tree-sitter grammars are not suitable for static analyzers---they serve entirely different purposes. For PHP you'd be better off using PHPStan or Mago. If you're still wanting to build your own tools then look into PHP-Parser or the Mago package for parsing PHP code

  2. added a commit that references this issue on Nov 27, 2026
    81ff3f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions