Skip to content

test: prove the operators slice in the engine, and order sortedBy keys as PHP#'s <=> does - #82

Merged
heyJordanParker merged 9 commits into
masterfrom
feat/slice-operators-engine
Oct 9, 2026
Merged

heyJordanParker merged 9 commits into
masterfrom
feat/slice-operators-engine

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Problem

The operators slice is in the checker, but the engine never proved it end to end. Nothing showed that a declared operator, a lifted ==, a bitwise flag test or a byte-ordered string comparison compiles to the PHP the slice intends and runs the same in every mode.

One behaviour was wrong. sortedBy ordered its keys with zend_compare, PHP's <=>:

  • A List<Line> sorted by a Money price followed PHP's property-by-property object order instead of the operator <=> that Money declares.
  • A string key followed PHP's order too, which compares two numeric strings as numbers, so "9" sorted before "10" where PHP#'s byte order puts "10" first.

Solution

Six commits, each test-first, plus a merge of master.

  1. The engine pins Mago master. After master released 0.3.0 and Mago merged docs: the spec states Jordan's specimen rulings, no type nests, and 12 examples lose their bugs #106, the pin is a7117a5. sharp_unit.h is the header that commit generates, and only SHARP_MAGO_COMMIT moves.

  2. Operators.sharp holds the slice as one PHP# file, and Operators.inc is its twin, written line for line. opcodes.phpt prints Operators: same opcodes and lines in 68 op arrays, same signatures in 8 classes.

  3. operators_refusal.phpt compiles a temporary project, as compile_command.phpt does, and shows 18 refused lines, each with its message, in file order.

  4. operator_results.phpt and its function and tracing JIT twins run every behaviour of the slice. sharp_collection_compare_keys now orders keys as PHP#'s <=> does:

    if (comparison) {
    	zend_call_known_function(comparison, NULL, scope, &order, 2, keys, NULL);
    	result = Z_TYPE(order) == IS_LONG ? ZEND_NORMALIZE_BOOL(Z_LVAL(order)) : 0;
    } else if (Z_TYPE_P(left) == IS_STRING && Z_TYPE_P(right) == IS_STRING) {
    	result = ZEND_NORMALIZE_BOOL(zend_binary_zval_strcmp(left, right));
    } else {
    	result = zend_compare(left, right);
    }

    Two object keys call the op_Comparison the left key's class declares or inherits, else the right key's. That is the class Mago picks for <. Two strings compare by their bytes, and every other key keeps zend_compare. An op_Comparison that throws leaves its exception for sortedBy to throw.

  5. operators_refusal.phpt adds the four orderings docs: the spec states Jordan's specimen rulings, no type nests, and 12 examples lose their bugs #106 refuses: < on two bools, < and <=> on two enum cases, and < on two lists.

  6. Operators.sharp gains Invoice and InvoiceQueue, whose next() counts its calls. The tests run this.next().total /= 2, this.next().balance /= 2 through Money's operator /, and this.next().total /= (this.next().total /= 2). Each call receiver runs once: the counter reads 1, 2 and 4, and the totals are 20, 450 EUR and 2. Compiled by Mago 7fdd901, before docs: the spec states Jordan's specimen rulings, no type nests, and 12 examples lose their bugs #106, the three operator_results tests stopped with Cannot use temporary expression in write context on line 331.

ext/sharp/
├── sharp.c                               <- sharp_collection_compare_keys
└── sharp_unit.h                          <- SHARP_MAGO_COMMIT a7117a519
Zend/tests/sharp/
├── Operators.sharp                       <- the operators slice as one PHP# file
├── Operators.inc                         <- its PHP twin
├── harness/Billing.inc                   <- the plain PHP classes it links against
├── opcodes.phpt                          <- Operators joins the fixture list
├── operators_refusal.phpt                <- 18 refused lines, each with its message
├── operator_results.inc                  <- runs every operator 100 times
├── operator_results.phpt                 <- without the JIT
├── operator_results_jit_function.phpt    <- under the function JIT
└── operator_results_jit_tracing.phpt     <- under the tracing JIT

Verification

Before the fix, the three operator_results tests printed b c a for the Money key and 9 10 Zebra apple for the string keys. They now print c a b and 10 9 Zebra apple.

The same tests show the rest of the slice already running:

  • "1" == "01" and "1e3" == "1000" are false.
  • 1 == 1.0 is true.
  • "10" < "9" is true.
  • A negative shift throws ArithmeticError, and 1 << 64 is 0.
  • Flags work with |, &, ^, ~ and %=.
  • Every operator Money declares runs.
  • price == null and price != zero on a null Money? run no Money ==, counted by the operator itself.

The full gate passes:

lockf /tmp/sharp-php-suite.lock sharp/bin/test --opcache --repeat --passes --census --differential
  • All 22 modes print Tests failed : 0 and Tests passed : 165.
  • The census reports no failure.
  • The differential check prints COMPILES for every .sharp file, Operators.sharp included.

…rs slice

Problem:
The engine was pinned to the mago-sharp 0.2.0 release, 98d1238.
The operators slice is in the checker on master: declared operators
run as static op_* methods, == is lifted over null, bitwise
operators take int only, and strings order by bytes. The engine
cannot prove that slice end to end while its .sharpc files come from
an older checker.

Solution:
- sharp_unit.h is the header Mago 7fdd901 generates. Only
  SHARP_MAGO_COMMIT moves. sharp_kind, the SHARP_T_ tokens and
  SHARP_UNIT_ABI stay the same, so the engine reads the same .sharpc
  layout.
- The PHP# suite passes unchanged: 161 of 161 without opcache.

ext/sharp/
└── sharp_unit.h   <- SHARP_MAGO_COMMIT 7fdd901
Problem:
The checker holds the operators slice, but no engine test compiled a
PHP# file that uses it. Nothing proved that a declared operator, a
lifted ==, a bitwise flag test or a pattern lowers to the opcodes,
lines and signatures a hand-written PHP file compiles to.

Solution:
- Operators.sharp declares every overloadable operator on Money, ==
  on an abstract DatabaseEntity that Order inherits, and methods for
  strict ==, an int against a float, byte-ordered strings, lifted
  == and != on Money?, every bitwise operator with %=, sortedBy, and
  the patterns, is and as forms of the slice.
- Operators.inc is its twin, written line for line. opcodes.phpt
  prints "Operators: same opcodes and lines in 62 op arrays, same
  signatures in 6 classes".
- harness/Billing.inc holds the plain PHP classes the fixture tests
  against: the Status enum, the payments, the responses and the
  shapes.

Zend/tests/sharp/
├── Operators.sharp       <- the operators slice as one PHP# file
├── Operators.inc         <- its PHP twin
├── harness/Billing.inc   <- the plain PHP classes it links against
└── opcodes.phpt          <- Operators joins the fixture list
Problem:
The checker refuses operator, equality, condition and pattern lines
that PHP# gives no meaning, but no engine test showed what a
developer sees when one of them reaches the engine.

Solution:
- operators_refusal.phpt compiles a temporary project that holds
  Operators.sharp and harness/Billing.inc, adds Refusals.sharp, and
  requires it with sharp.compile_command set, as
  compile_command.phpt does.
- The engine shows 14 refusals in file order: == on a class without
  operator ==, operator == without hash(), an int condition in if
  and in while, | on two bools, a match that misses an enum case, a
  match on an int without default, a property used after is, a
  pattern variable used outside its test, !x is T, not beside or,
  is int?, a pattern that never matches, and an unparenthesized
  nested ternary.

Zend/tests/sharp/
└── operators_refusal.phpt   <- 14 refused lines, each with its message
Problem:
sharp_collection_compare_keys ordered two sortedBy keys with
zend_compare, PHP's <=>. A List<Line> sorted by a Money price
followed PHP's property-by-property object order instead of the
operator <=> Money declares. A string key followed PHP's order too,
which compares two numeric strings as numbers, so "9" sorted before
"10" where PHP#'s byte order puts "10" first.

Solution:
- Two object keys compare with the op_Comparison the left key's
  class declares or inherits, else the right key's, which is the
  class Mago picks for <. Two string keys compare by their bytes.
  Every other key keeps zend_compare. Equal keys keep their order.
- An op_Comparison that throws leaves its exception for sortedBy to
  throw, as a key selector that throws does.
- operator_results.phpt runs Operators.sharp: strict == on strings,
  an int against a float, byte-ordered <, a negative shift that
  throws ArithmeticError, 1 << 64 as 0, flags with | & ^ ~ and %=,
  every operator Money declares, and == and != on a null Money?
  without running Money's ==, counted by the operator itself. Its
  function JIT and tracing JIT twins expect the same output.
- Before the fix the three tests printed "b c a" for the Money key
  and "9 10 Zebra apple" for the string keys. They now print
  "c a b" and "10 9 Zebra apple".

ext/sharp/
└── sharp.c                               <- sharp_collection_compare_keys
Zend/tests/sharp/
├── operator_results.inc                  <- runs every operator 100 times
├── operator_results.phpt                 <- without the JIT
├── operator_results_jit_function.phpt    <- under the function JIT
└── operator_results_jit_tracing.phpt     <- under the tracing JIT
Problem:
master released php-sharp 0.3.0, pinned to mago-sharp 58c7c4a, while
this branch pinned Mago 7fdd901. Both pins changed the same line of
sharp_unit.h. Mago master has since merged #106 at a7117a5, which
holds both commits.

Solution:
- sharp_unit.h is the header Mago a7117a5 generates. Only
  SHARP_MAGO_COMMIT moves. sharp_kind, the SHARP_T_ tokens and
  SHARP_UNIT_ABI stay the same.
- mago compile accepts all 97 .sharp files, and the PHP# suite passes
  165 of 165 without opcache.

ext/sharp/
└── sharp_unit.h   <- SHARP_MAGO_COMMIT a7117a5
… and a collection

Problem:
Mago #106, merged at a7117a5, refuses <, <=, >, >= and <=> when a
side holds a bool, an enum case or a collection. PHP orders two enum
cases as always false, so that ordering was a silent bug. No engine
test showed the refusal a developer now sees.

Solution:
- Refusals.sharp in operators_refusal.phpt gains four lines: < on two
  bools, < and <=> on two Status cases, and < on two List<int>.
- The engine shows 18 refusals in file order, each new one as
  "`<` orders numbers, strings and classes that declare
  `operator <=>`, and `bool` is none of them."

Zend/tests/sharp/
└── operators_refusal.phpt   <- 18 refused lines, 4 of them orderings
…wers it

Problem:
Before Mago #106, this.next().total /= 2 lowered to
($receiver#1 = $this->next())->total = \intdiv($receiver#1->total, 2),
which PHP refuses with "Cannot use temporary expression in write
context". The pin a7117a5 carries the fix, but no engine test ran
it.

Solution:
- Operators.sharp gains Invoice and InvoiceQueue, whose next() counts
  its calls. halve runs this.next().total /= 2, halveBalance runs
  this.next().balance /= 2 through Money's operator /, and halveTwice
  runs this.next().total /= (this.next().total /= 2).
- Operators.inc writes each as Mago lowers it, as
  ${'receiver#1'}->total = \intdiv((${'receiver#1'} = $this->next())->total, 2),
  with ${'receiver#2'} for the inner /=. opcodes.phpt prints
  "Operators: same opcodes and lines in 68 op arrays, same signatures
  in 8 classes".
- operator_results.inc prints each total and the call count after it:
  20 after one call, 450 EUR after two, and 2 after four, since the
  outer read takes 20 before the inner /= leaves 10.
- Compiled by Mago 7fdd901, before #106, the three operator_results
  tests stopped with "Cannot use temporary expression in write
  context" on line 331. Compiled by a7117a5 they pass.

Zend/tests/sharp/
├── Operators.sharp                       <- Invoice, InvoiceQueue
├── Operators.inc                         <- their twin
├── opcodes.phpt                          <- 68 op arrays, 8 classes
├── operator_results.inc                  <- totals and call counts
├── operator_results.phpt                 <- without the JIT
├── operator_results_jit_function.phpt    <- under the function JIT
└── operator_results_jit_tracing.phpt     <- under the tracing JIT
Problem:
When an op_Comparison throws, the exception is raised inside
zend_hash_sort. sharp_collection_compare_keys keeps being called with
the exception pending and returns 0 each time, because the returned
value stays IS_UNDEF. No test showed that this path neither crashes
nor leaks, and that the caller of sortedBy sees the exception.

Solution:
- Operators.sharp gains Grade, whose operator <=> throws Ungraded for
  a negative score, and Grade.ranked, which sorts by the grade itself.
  harness/Billing.inc declares Ungraded, so the fixture needs no
  import and no line above it moves.
- operator_results.inc sorts 3, -1 and 2, and prints
  "Billing\Ungraded: a negative grade has no order in Operators.sharp
  on line 360". It then sorts 3, 1 and 2 and prints "1 2 3". It runs
  100 times without the JIT and under the function and tracing JITs,
  and the LEAK and --repeat gates check it.
- Operators.inc is the twin. opcodes.phpt prints "Operators: same
  opcodes and lines in 72 op arrays, same signatures in 9 classes".

Zend/tests/sharp/
├── Operators.sharp                       <- Grade
├── Operators.inc                         <- its twin
├── harness/Billing.inc                   <- Ungraded
├── opcodes.phpt                          <- 72 op arrays, 9 classes
├── operator_results.inc                  <- a sort that throws, then one that does not
├── operator_results.phpt                 <- without the JIT
├── operator_results_jit_function.phpt    <- under the function JIT
└── operator_results_jit_tracing.phpt     <- under the tracing JIT
@heyJordanParker
heyJordanParker merged commit 480ceab into master Oct 9, 2026
6 checks passed
heyJordanParker added a commit that referenced this pull request Oct 10, 2026
…010d53

Master pins Mago a59162f, the merge of mago-sharp#110, and this branch pinned b68a9c4, the head of mago-sharp#101. Neither commit holds the other's checker. Mago master 4010d53, the merge of mago-sharp#113, holds #101 and #110, so the merge pins it. The header that the bridge at 4010d53 builds matches this file exactly, so only `SHARP_MAGO_COMMIT` moves and `SHARP_UNIT_ABI` is unchanged.

ext/sharp/
  sharp_unit.h   SHARP_MAGO_COMMIT is 4010d53
@heyJordanParker
heyJordanParker deleted the feat/slice-operators-engine branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant