Skip to content

Language: foreach on array literal with enum case key — must TypeError not coerce to backing scalar (Zend/zend_hash.c) #10363

Description

@PurHur

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

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 20, 2026
  2. PurHur commented on Jun 20, 2026

    @PurHur
    OwnerAuthor

    Closing as duplicate of #9512 (array literal enum case keys → backing scalar). Foreach repro foreach ([E::A => 1] as $k => $v) belongs in #9512 done-when / verification matrix — no separate worker claim needed.

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

    IMPORTANTCritical patharea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language featuresstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions