Repository navigation
Carry narrowings through tracked comparison verdicts - #7233
Open
ondrejmirtes wants to merge 1 commit into
Open
ondrejmirtes wants to merge 1 commit into
ondrejmirtes wants to merge 1 commit into
Conversation
ondrejmirtes
force-pushed
the
relational-comparison-facts
branch
from
November 27, 2026 21:52
d8381c0 to
a761ed4
Compare
ondrejmirtes
marked this pull request as ready for review
November 27, 2026 21:53
Collaborator
|
This pull request has been marked as ready for review. |
ondrejmirtes
force-pushed
the
relational-comparison-facts
branch
from
November 27, 2026 22:02
a761ed4 to
5aac852
Compare
A narrowed `$a === $b` or integer `$a < $b` narrowed each operand
once, against the other one's type at that moment. A later narrowing of
one operand did not reach the other, so the order of the conditions
mattered: `if ($b > 0) { if ($a === $b) … }` gave `int<1, max>`, the
reverse order `int`.
The verdict of such a comparison between variables, properties, offsets
or count() is now kept in the scope, and applySpecifiedTypes() narrows
the other operand when one operand narrows. A count() that cannot be 0
any more makes its array non-empty.
RuleTestCase compared two strings it had just checked to be identical;
the two early returns are merged.
Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
ondrejmirtes
force-pushed
the
relational-comparison-facts
branch
from
November 27, 2026 22:29
5aac852 to
ef1d762
Compare
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.
A narrowed
$a === $bor integer$a < $bused to narrow each operand once, using the other operand's type at that moment. Nothing remembered the relation, so a later narrowing of one operand never reached the other, and the order of conditions mattered:$i >= 0 && count($l) > $i→$lnon-empty-list<int>list<int>non-empty-list<int>if ($b > 0) { if ($a === $b) … }→$aint<1, max>intint<1, max>if ($a >= 0) { if ($a < $b) … }→$bint<1, max>intint<1, max>Changes:
BinaryOpHandleradds the comparison's verdict to theSpecifiedTypesof===(true),!==(false) and integer<,<=,>,>=. Operands must be variables, property or offset fetches, orcount()/sizeof()of those, and neither operand may have a single finite value (a constant operand's own narrowing is all there is).MutatingScope::applySpecifiedTypes()then looks for tracked verdicts with a narrowed operand and narrows the other operand: same type for identity, shifted range for</<=. This runs up toRELATIONAL_FACT_ROUNDS_LIMIT(4) rounds, so$a < $b && $b < $creaches$c. Acount()that can no longer be 0 makes its array non-empty.RuleTestCaseasserted that two strings it had just compared with===were the same. The new narrowing reports that, so the two early returns are merged into one.TypeSpecifierTest:$foo === $barnow also lists the verdict.nsrt/relational-comparison-facts.phphas the seven order-dependent cases, which fail on base, and ten controls: reassignment,!==, a false<, floats, a merge,count() <=, and so on. It also has five cases taken from the new integration errors below, which fail on base too.The C++ mirrors of
BinaryOpHandler(rememberRelationalFact(),isRelatableOperand()) andMutatingScope(deriveFromRelationalFacts()and the rounds inapplySpecifiedTypes()) are ported. Locally: the full suite passes with and without the extension, walk-trace is identical, and signature parity and side-by-side pass.Cost:
deriveFromRelationalFacts()scans the tracked expressions on each application. On the benchmarks I tried (bug-10538,bug-14624,bug-7903) timings are within noise; a synthetic file with 400 stored conditions is about 7% slower.CI: the Issue bot reports no changes in 1216 snippets. Compared with #7232's run, the integration jobs show six new errors, all true positives:
wp-includes/functions.php:298:if ( $i < $format_length )insidefor ( $i = 0; $i < $format_length; $i++ ), just before$format[ ++$i ]. The guard is always true, and the code has a real off-by-one bug: it should check$i + 1.DisallowYodaComparisonSniff.php:58: afterif ($leftDynamism >= $rightDynamism) return;, the check$leftDynamism >= 900 && $rightDynamism >= 900has a redundant right side.LineLengthSniff.php:97: it checks$lineLength > $this->lineLengthLimitagain after returning on<=.Stats.php:1600:$diff < $skip_time || ($diff < $skip_time && …)repeats the left condition.RawPostParserIsolatedTest.php:109: a secondassertSame($first, $second)right after the first.The other integration differences (laravel
TestEnum, neosarray_reduce) also fluctuate between runs without this change.