Skip to content

Stdlib: usort()/uasort()/uksort() — enum case arrays must keep enum objects not backing scalars (ext/standard/array.c) #8867

Description

@PurHur

Category

stdlib — php-src-strict

Problem

Sorting functions that reorder array elements in-place must preserve enum case objects when the array holds enum cases. sort() is tracked in #5691; usort(), uasort(), and uksort() are not and today replace elements with backing scalars after the user callback returns.

php-src reference

Repro

<?php
enum E: int { case A = 1; case B = 2; }
$a = [E::B, E::A];
usort($a, fn($x, $y) => $x <=> $y);
var_export($a);
// Zend: [E::A, E::B]
// VM today: [1, 2]
./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::B,E::A]; usort(\$a, fn(\$x,\$y)=>\$x<=>\$y); var_export(\$a);"'

Repeat for uasort() (preserve keys) and uksort() (keys are enum cases in #8779 family).

Scope (this repo)

Piece Path
VM sort helpers ext/standard/VmArraySort.php or array builtin handlers for usort/uasort/uksort
Enum preservation lib/VM/EnumCaseSupport.php — shared with #5691 / #5547
JIT lib/JIT/Builtin/ArraySort.php (if lowered)

Done when

Verify

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter usort_enum'

Links

#1492 · #5691 · #5547 · #5585 · #8746

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-2:languagePhase 2 – language featuresphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions