Skip to content

Carry narrowings through tracked comparison verdicts - #7233

Open
ondrejmirtes wants to merge 1 commit into
2.3.xfrom
relational-comparison-facts
Open

ondrejmirtes wants to merge 1 commit into
2.3.xfrom
relational-comparison-facts

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

A narrowed $a === $b or integer $a < $b used 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:

Code Before Reverse order, before Now (both orders)
$i >= 0 && count($l) > $i → $l non-empty-list<int> list<int> non-empty-list<int>
if ($b > 0) { if ($a === $b) … } → $a int<1, max> int int<1, max>
if ($a >= 0) { if ($a < $b) … } → $b int<1, max> int int<1, max>

Changes:

  • BinaryOpHandler adds the comparison's verdict to the SpecifiedTypes of === (true), !== (false) and integer <, <=, >, >=. Operands must be variables, property or offset fetches, or count()/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 to RELATIONAL_FACT_ROUNDS_LIMIT (4) rounds, so $a < $b && $b < $c reaches $c. A count() that can no longer be 0 makes its array non-empty.
  • Assigning an operand already invalidates the verdict, because its key contains the operand, and branch merges keep it only when both branches have it.
  • RuleTestCase asserted 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 === $bar now also lists the verdict.

nsrt/relational-comparison-facts.php has 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()) and MutatingScope (deriveFromRelationalFacts() and the rounds in applySpecifiedTypes()) 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:

  • WordPress wp-includes/functions.php:298: if ( $i < $format_length ) inside for ( $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.
  • slevomat DisallowYodaComparisonSniff.php:58: after if ($leftDynamism >= $rightDynamism) return;, the check $leftDynamism >= 900 && $rightDynamism >= 900 has a redundant right side.
  • slevomat LineLengthSniff.php:97: it checks $lineLength > $this->lineLengthLimit again after returning on <=.
  • ampache Stats.php:1600: $diff < $skip_time || ($diff < $skip_time && …) repeats the left condition.
  • openemr RawPostParserIsolatedTest.php:109: a second assertSame($first, $second) right after the first.

The other integration differences (laravel TestEnum, neos array_reduce) also fluctuate between runs without this change.

@ondrejmirtes
ondrejmirtes force-pushed the relational-comparison-facts branch from d8381c0 to a761ed4 Compare November 27, 2026 21:52
@ondrejmirtes
ondrejmirtes marked this pull request as ready for review November 27, 2026 21:53
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes
ondrejmirtes force-pushed the relational-comparison-facts branch from a761ed4 to 5aac852 Compare November 27, 2026 22:02
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
ondrejmirtes force-pushed the relational-comparison-facts branch from 5aac852 to ef1d762 Compare November 27, 2026 22:29
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.

2 participants