Skip to content

Stdlib: sort()/rsort() reject SORT_NUMERIC and other SORT_* named constants at runtime (ext/standard/array.c) #9429

Description

@PurHur

Category

stdlib · VM builtin args · php-src-strict

Sibling: #9278 (bitwise SORT_NATURAL|SORT_FLAG_CASE expressions on ksort/asort). This issue is the common case: passing a named sort constant (SORT_NUMERIC, SORT_STRING, …) as the flags argument to sort() / rsort().

Problem

sort($array, SORT_NUMERIC) throws LogicException: sort() flags must be an integer in this compiler build even though var_dump(SORT_NUMERIC) is int(1) and Zend sorts correctly.

VmInternalCompare::resolveFrameSortFlags() requires Variable::TYPE_INTEGER on the call argument slot; constants like SORT_NUMERIC are not materialized as integer-typed frame operands at the callsite (same class of bug as #9278, but hits sort_ / rsort_ and the most common stdlib sort flags).

php-src reference

  • ext/standard/array.c — php_array_sort() / php_sort() flag parsing
  • ext/standard/php_sort.h — SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_NATURAL, SORT_FLAG_CASE

Repro

php test/repro/maintainer_gap_sort_sort_numeric_constant.php   # Zend: 1,2,10
php bin/vm.php test/repro/maintainer_gap_sort_sort_numeric_constant.php  # LogicException

Also fails for rsort($a, SORT_NUMERIC); asort/ksort family may share resolveFrameSortFlags().

Done when (php-src-strict)

  • sort() / rsort() accept SORT_* named constants and literal int flags at runtime on VM.
  • SORT_NUMERIC sorts string elements numerically (['10','2','1'] → 1,2,10).
  • JIT/AOT: VmInternalCompare::resolveJitSortFlags() path stays aligned when in scope.
  • Compliance .phpt under test/compliance/cases/stdlib/.

Implementation hints

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 appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions