Skip to content

Stdlib: array_reduce() on enum case arrays — closure callbacks must preserve enum objects (ext/standard/array.c) #5626

Description

@PurHur

Category

stdlib

Problem

array_reduce() with a closure callback over [E::A, E::B] must return the enum case object selected by the callback (Zend). This compiler rejects closure callbacks with LogicException (array_reduce() callback must be a string user-function name). Even after #3086 enables closure callbacks, enum arrays must not coerce case objects to backing scalars between folds.

php-src reference

Repro

<?php
enum E: int { case A = 1; case B = 2; }
$cases = [E::A, E::B];
$r = array_reduce($cases, function ($carry, $item) {
    return $carry === null ? $item : $item;
});
var_export($r);
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro.php
php bin/vm.php repro.php
'
Runtime Expected
Zend E::A (enum object)
VM today LogicException (closure deferred)

Scope

Done when

  • Repro prints enum case object identity on VM
  • ./script/ci-fast.sh --filter array_reduce_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