Category
language · php-src-strict
Problem
#9512 tracks array literal construction coercing enum case keys to backing scalars. A related runtime symptom: foreach ([E::A => 1] as $k => $v) on this compiler iterates with $k as the backing int 1. Zend rejects the array at runtime with TypeError: Illegal offset type when the illegal key is encountered (literal construction path).
Workers should fix literal key rejection (#9512) and ensure foreach never exposes materialized scalar keys for enum-case-keyed arrays.
php-src reference
Repro
<?php
declare(strict_types=1);
enum E: int { case A = 1; }
foreach ([E::A => 1] as $k => $v) {
var_export($k);
echo "\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php -r "enum E: int { case A=1; } foreach ([E::A=>1] as \$k=>\$v) { var_export(\$k); echo PHP_EOL; }" 2>&1 | head -3
php bin/vm.php -r "enum E: int { case A=1; } foreach ([E::A=>1] as \$k=>\$v) { var_export(\$k); echo PHP_EOL; }" 2>&1 | head -3
'
| Runtime |
Result |
| Zend |
TypeError: Illegal offset type |
| VM |
prints 1 (backing scalar key) ❌ |
Scope
| Path |
Work |
lib/Compiler.php / array literal lowering |
reject enum case keys (#9512) |
lib/VM.php foreach |
do not materialize enum keys to scalars |
| Tests |
test/compliance/cases/language/foreach_enum_case_key_typeerror.phpt (may extend existing runtime case) |
Done when
Related
Category
language· php-src-strictProblem
#9512 tracks array literal construction coercing enum case keys to backing scalars. A related runtime symptom:
foreach ([E::A => 1] as $k => $v)on this compiler iterates with$kas the backing int1. Zend rejects the array at runtime withTypeError: Illegal offset typewhen the illegal key is encountered (literal construction path).Workers should fix literal key rejection (#9512) and ensure foreach never exposes materialized scalar keys for enum-case-keyed arrays.
php-src reference
Zend/zend_hash.c— illegal offset type for enum/object keysZend/zend_execute.c—ZEND_FE_FETCH_Rkey zvalRepro
TypeError: Illegal offset type1(backing scalar key) ❌Scope
lib/Compiler.php/ array literal loweringlib/VM.phpforeachtest/compliance/cases/language/foreach_enum_case_key_typeerror.phpt(may extend existing runtime case)Done when
TypeError: Illegal offset typeon VM (and JIT when lowered)[E::A => v]also TypeErrors at compile or first runtime use per Zend./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter foreach_enum_case_key'greenRelated