[php-src] PR #20574: [RFC] Pattern matching
| From: | iluuu1994 | Date: | Mon, 24 Nov 2025 15:47:27 +0000 |
| Subject: | [php-src] PR #20574: [RFC] Pattern matching | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-34141@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/20574
Author: iluuu1994
https://wiki.php.net/rfc/pattern-matching
Known issues:
- [ ] Array elements are fetched with
FETCH_DIM_IS to avoid erroring for accessign
non-existent indexes. Because undefined indexes evalute to null, ['a'
=> 42] is ['b' => null] will evaluate to true as a side-effect.
This may be solved by returning IS_UNDEF from FETCH_DIM_IS. This may break
some expectations, as we currently don't store IS_UNDEF in VARs.
Further technical considerations:
- The implementation is currently emitting various COPY_TMP ops. These could be avoided
by creating specialized ops that don't free op1. This is the case for:
- QM_ASSIGN for bindings. This one may be avoidable by pulling bindings to the
tail.
- TYPE_CHECK for array patterns. Alternatively we could use the new
HAS_TYPE opcode.
- COUNT for array patterns.
- FETCH_DIM_IS for array patterns.
- INSTANCEOF for object patterns. Alternatively we could use the new
HAS_TYPE opcode.
- FETCH_OBJ_R for object patterns.
- mixed could be a no-op. This has some some implications on branching, so it might be
enough to let the optimizer elide it (which already happens).
- The new HAS_TYPE opcode allows performing arbitrary type checks. However, pattern
matching doesn't currently combine these types discovered at compile time. I.e.
int|float should be a simple type mask, but currently compiles to two
HAS_TYPE checks.
- The implementaiton currently uses JMP[N]Z_EX to immediately redeclare
result which it consumes. This is mostly unnecessary, as bools aren't
consumed at all. The other side-effect is that JMP[N]Z_EX isn't a smart branch.
This is relevant because smart branches skip assigning to result, which is necessary
here. We may be able to use JMP[N]Z by explicitly removing the smart branch flags, and
making sure the optimizer doesn't re-add them.
- CVs are currently used directly. However, some operations (currently only
FETCH_DIM_R) may mutate CVs as a side-effect (e.g. in get through
references, or globals). IMO, guarding against this is not worth it, as that would require an
initial copy and subsequent COPY_TMPs. It's just worth pointing out.