Skip to content

Stdlib: sort() on enum case arrays throws LogicException — must sort by backing like Zend (ext/standard/array.c) #5691

Description

@PurHur

Category

stdlib

Problem

Zend sort([E::B, E::A]) on backed enum cases sorts in place (by backing value) and leaves enum case objects in the array (A then B for A=1, B=2).

This compiler VM throws LogicException: sort() only supports homogeneous string or integer arrays in this compiler build — no sort happens.

Related: #5546 tracks coercion to scalars after sort; this issue is the earlier failure mode (hard reject vs Zend acceptance).

php-src reference

Repro

docker info >/dev/null
./script/docker-exec.sh -- bash -lc '
php -r "enum E: int { case A = 1; case B = 2; } \$a = [E::B, E::A]; sort(\$a); foreach (\$a as \$v) echo \$v->name, \"\\n\";"
php bin/vm.php -r "enum E: int { case A = 1; case B = 2; } \$a = [E::B, E::A]; sort(\$a); foreach (\$a as \$v) echo \$v->name, \"\\n\";"
'

Zend: A then B
VM: LogicException before loop

Scope (PHP-in-PHP)

Layer Path
VM ext/standard/sort_.php, lib/VM/EnumCaseSupport.php — allow enum case arrays; compare via compareEnumCasesForMinMax
JIT lib/JIT/Builtin/ sort lowering when element type is enum case
Tests test/compliance/cases/stdlib/sort_enum_cases.phpt

Done when

  • Repro prints A/B on VM without exception
  • ./script/ci-fast.sh --filter sort_enum green

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