Category
language — php-src-strict enum operators
Problem
Backed enum cases are not silently coerced to their backing scalar for +, -, *, /, %, and related numeric operators. Zend throws TypeError: Unsupported operand types: E + int.
This compiler evaluates E::A + 1 as 2 when E::A backs 1, breaking enum operator parity.
Related but narrower: #8972 (array_reduce accumulator), #9546 (===).
php-src reference
Repro (failure today)
Save as test/repro-maintainer/enum_numeric_arithmetic.php:
<?php
declare(strict_types=1);
enum E: int { case A = 1; }
foreach (['+', '-', '*'] as $op) {
try {
$expr = 'E::A ' . $op . ' 1';
echo $expr, '=', eval('return ' . $expr . ';'), "\n";
} catch (Throwable $e) {
echo $expr, ' ', get_class($e), "\n";
}
}
Minimal single-op repro:
<?php
enum E: int { case A = 1; }
try { echo E::A + 1; } catch (Throwable $e) { echo get_class($e), "\n"; }
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/enum_numeric_arithmetic.php
php bin/vm.php test/repro-maintainer/enum_numeric_arithmetic.php'
| Check |
Zend |
VM (today) |
E::A + 1 |
TypeError |
prints 2 ❌ |
E::A - 1 |
TypeError |
coerced math ❌ |
E::A * 2 |
TypeError |
coerced math ❌ |
Scope (PHP-in-PHP)
| Layer |
Path |
| VM |
lib/VM/Variable.php binary ops / lib/VM.php opcode handlers — reject enum+scalar before backing coercion |
| JIT/AOT |
lib/JIT/JitValueCompare.php, arithmetic lowering — same guard |
| Shared |
central enum-operand helper (pair with #9546) |
Done when
Related
Category
language— php-src-strict enum operatorsProblem
Backed enum cases are not silently coerced to their backing scalar for
+,-,*,/,%, and related numeric operators. Zend throwsTypeError: Unsupported operand types: E + int.This compiler evaluates
E::A + 1as2whenE::Abacks1, breaking enum operator parity.Related but narrower: #8972 (
array_reduceaccumulator), #9546 (===).php-src reference
Zend/zend_operators.c—add_function,sub_function, … enum operand guardsZend/zend_enum.c— enum zval typeRepro (failure today)
Save as
test/repro-maintainer/enum_numeric_arithmetic.php:Minimal single-op repro:
E::A + 1TypeError2❌E::A - 1TypeErrorE::A * 2TypeErrorScope (PHP-in-PHP)
lib/VM/Variable.phpbinary ops /lib/VM.phpopcode handlers — reject enum+scalar before backing coercionlib/JIT/JitValueCompare.php, arithmetic lowering — same guardDone when
TypeErrorfor+,-,*on VMtest/compliance/cases/language/enum_numeric_arithmetic.phptgreenruntime/*.cRelated