Repository navigation
test: prove the operators slice in the engine, and order sortedBy keys as PHP#'s <=> does - #82
Merged
Merged
Conversation
…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
enabled auto-merge
October 9, 2026 22:13
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
sortedByordered its keys withzend_compare, PHP's<=>:List<Line>sorted by aMoneyprice followed PHP's property-by-property object order instead of theoperator <=>thatMoneydeclares."9"sorted before"10"where PHP#'s byte order puts"10"first.Solution
Six commits, each test-first, plus a merge of master.
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.his the header that commit generates, and onlySHARP_MAGO_COMMITmoves.Operators.sharpholds the slice as one PHP# file, andOperators.incis its twin, written line for line. opcodes.phpt printsOperators: same opcodes and lines in 68 op arrays, same signatures in 8 classes.operators_refusal.phptcompiles a temporary project, as compile_command.phpt does, and shows 18 refused lines, each with its message, in file order.operator_results.phptand its function and tracing JIT twins run every behaviour of the slice.sharp_collection_compare_keysnow orders keys as PHP#'s<=>does:Two object keys call the
op_Comparisonthe 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 keepszend_compare. Anop_Comparisonthat throws leaves its exception forsortedByto throw.operators_refusal.phptadds the four orderings docs: the spec states Jordan's specimen rulings, no type nests, and 12 examples lose their bugs #106 refuses:<on twobools,<and<=>on two enum cases, and<on two lists.Operators.sharpgainsInvoiceandInvoiceQueue, whosenext()counts its calls. The tests runthis.next().total /= 2,this.next().balance /= 2throughMoney's operator/, andthis.next().total /= (this.next().total /= 2). Each call receiver runs once: the counter reads 1, 2 and 4, and the totals are 20,450 EURand 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 threeoperator_resultstests stopped withCannot use temporary expression in write contexton line 331.Verification
Before the fix, the three
operator_resultstests printedb c afor theMoneykey and9 10 Zebra applefor the string keys. They now printc a band10 9 Zebra apple.The same tests show the rest of the slice already running:
"1" == "01"and"1e3" == "1000"are false.1 == 1.0is true."10" < "9"is true.ArithmeticError, and1 << 64is0.|,&,^,~and%=.Moneydeclares runs.price == nullandprice != zeroon a nullMoney?run noMoney==, counted by the operator itself.The full gate passes:
Tests failed : 0andTests passed : 165.COMPILESfor every.sharpfile,Operators.sharpincluded.