Skip to content

Stdlib: array_map()/array_filter() on enum arrays must preserve enum case objects (ext/standard/array.c) #5564

Description

@PurHur

Category

stdlib

Problem

array_map() and array_filter() must pass enum case objects through to callbacks and return arrays whose elements remain enum cases when the callback returns them unchanged. This compiler unwraps enum cases to backing scalars (int/string) during iteration, so identity callbacks produce numeric arrays.

php-src reference

Repro (verified 2026-06-04)

<?php
enum E: int { case A = 1; case B = 2; }

var_export(array_map(fn($x) => $x, [E::A, E::B]));
echo "\n";
var_export(array_filter([E::A, E::B], fn($x) => $x === E::A));
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && cd /compiler && php repro.php && php bin/vm.php repro.php'
Builtin Zend VM today
array_map(fn($x)=>$x, …) array(0=>\E::A, 1=>\E::B) array(0=>1, 1=>2)
array_filter(…, fn…) array(0=>\E::A) array(0=>1)

Scope (this repo)

Area Path
VM ext/standard/array_map.php, array_filter.php, VmArray.php / iterator helpers
JIT lib/JIT/ArrayBuiltinHelper.php — enum-aware element copy
Tests test/compliance/cases/stdlib/array_map_enum_identity.phpt, array_filter_enum.phpt

Distinct from #5547 (array_values/array_merge) and #5546 (sort family) — those are separate builtins with the same root scalarization bug.

Done when

  • Repro A matches Zend on bin/vm.php
  • Callbacks receive enum objects ($x === E::A strict works inside closure)
  • JIT lowering matches VM for identity map/filter (or documented VM-only until phase 2)
  • ./script/ci-fast.sh --filter array_map_enum green

Related

Activity

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