Skip to content

Stdlib: array_first()/array_last() — enum case elements materialize backing scalars (ext/standard/array.c) #8987

Closed

Description

@PurHur

Category

stdlib | language

Problem

PHP 8.4 array_first() / array_last() must return the first/last array element as stored, including backed enum case objects. VM today materializes int-backed cases to their backing scalar (int(1) / int(2) instead of E::A / E::B).

Verified on master:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "enum E: int { case A=1; case B=2; } \$a=[E::A,E::B]; var_dump(array_first(\$a), array_last(\$a));"'
# int(1) int(2)  — wrong

Zend PHP 8.4+ returns enum objects (same identity as input elements). Distinct from #8899 (pointer walkers) and #6949 ($strict third parameter).

php-src reference

Repro

test/repro-maintainer/parity_array_first_last_enum.php:

<?php
enum E: int { case A = 1; case B = 2; }
$a = [E::A, E::B];
$first = array_first($a);
$last = array_last($a);
var_export($first === E::A);
echo "\n";
var_export($last === E::B);
echo "\n";
var_export($first instanceof E);
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/parity_array_first_last_enum.php
php bin/vm.php test/repro-maintainer/parity_array_first_last_enum.php'
Check Zend 8.4+ VM today
array_first([E::A, E::B]) === E::A true false (int 1)
array_last(...) === E::B true false (int 2)

Scope (PHP-in-PHP, php-src-strict)

Path Work
ext/standard/VmArray.php valueFirst() / valueLast() — preserve enum objects from HashTable iteration
lib/VM/HashTable.php If iterCurrentValue() / copyFrom scalarizes enums, fix shared path (reuse EnumCaseSupport guards)
ext/standard/array_first.php, array_last.php VM entry unchanged if helper fixed
lib/JIT/Builtin/ JIT lowering for array_first/array_last if enum boxes are coerced in IR
Tests test/compliance/cases/stdlib/array_first_enum.phpt

Done when

  • Repro prints true for all three var_export lines on bin/vm.php (php-src-strict)
  • Empty-array array_first([]) still returns null on VM
  • ./script/ci-fast.sh --filter array_first green (or targeted PHPUnit)
  • No new C in runtime/ — enum preservation belongs in PHP VM/JIT paths

Related

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 16, 2026
  2. PurHur commented on Jun 27, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-c — batch (issues #8987)

  3. PurHur commented on Jun 27, 2026

    @PurHur
    OwnerAuthor

    claim released: cursor-agent-lane-c — repro green on master (enum cases preserved); pivoting to #4403.

  4. PurHur commented on Jul 2, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-c — batch (issues #9263, #9212, #8987)

  5. PurHur commented on Jul 2, 2026

    @PurHur
    OwnerAuthor

    claim released: worker-lane-c — already green on master (enum identity repro prints true/true/true).

  6. PurHur commented on Jul 5, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-c — starting this run

  7. PurHur commented on Jul 7, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-C — starting this run

  8. PurHur commented on Jul 7, 2026

    @PurHur
    OwnerAuthor

    claim released: repro green on master with PHP_COMPILER_PROFILE=8.4 — array_first/last return enum(E::A)/enum(E::B); array_first_last_enum_jit.phpt passes VM+JIT.

  9. PurHur commented on Jul 13, 2026

    @PurHur
    OwnerAuthor

    Stale on master (lane C hygiene, 2026-07-13): repro green on Zend + VM. Verified via ./script/docker-exec.sh — no code change 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

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions