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
Category
stdlib— php-src-strictProblem
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(), anduksort()are not and today replace elements with backing scalars after the user callback returns.php-src reference
ext/standard/array.c—php_usort(),php_uasort(),php_uksort()(zval copy without enum unwrap)Repro
./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) anduksort()(keys are enum cases in #8779 family).Scope (this repo)
ext/standard/VmArraySort.phpor array builtin handlers forusort/uasort/uksortlib/VM/EnumCaseSupport.php— shared with #5691 / #5547lib/JIT/Builtin/ArraySort.php(if lowered)Done when
array_walkparity)test/compliance/cases/stdlib/usort_enum_case_preserve.phptgreenuasort/uksortenum-key variants covered or filed as follow-upsVerify
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter usort_enum'Links
#1492 · #5691 · #5547 · #5585 · #8746