Skip to content

Stdlib: array_rand() on enum case arrays materializes backing scalars (ext/standard/array.c) #8946

@PurHur

Description

@PurHur

Category

stdlib · php-src-strict

Problem

array_rand() on an array of enum case objects must return a key whose element remains an enum object. VM indexes the array and materializes the backing scalar (int/string) instead of preserving the enum case.

Verified 2026-06-16: Zend PHP 8.2 vs php bin/vm.php.

php-src reference

Repro

test/repro/maintainer_gap_array_rand_enum.php

php test/repro/maintainer_gap_array_rand_enum.php
php bin/vm.php test/repro/maintainer_gap_array_rand_enum.php
Zend VM (wrong)
get_debug_type($arr[array_rand($arr)]) E int
$value instanceof E true false
enum E: int { case A = 1; case B = 2; }
$arr = [E::A, E::B, E::A];
$key = array_rand($arr);
$value = $arr[$key]; // Zend: E::A|E::B; VM: 1|2

Scope (PHP-in-PHP)

Path Work
ext/standard/array_rand.php Stop enum→scalar coercion when returning elements / building result
lib/VM/ArrayOps.php (if shared) Preserve enum objects in array access paths used by array_rand
lib/JIT/Builtin/Standard/ArrayRand.php JIT parity when in scope
Tests test/compliance/cases/stdlib/array_rand_enum.phpt

Done when

  • VM: array_rand() on enum case arrays returns keys mapping to enum objects (same as Zend)
  • array_rand($arr, $num) multi-pick parity when applicable
  • Targeted PHPUnit/compliance guard green

Related

#5554 array_slice/reverse · #5586 array_pop/splice · #1492

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