Skip to content

Narrow a mixed subject of a loose comparison to mixed~null - #7061

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
fix-15725-loose-equal-mixed-not-null
Nov 14, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
fix-15725-loose-equal-mixed-not-null

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Closes phpstan/phpstan#15725

Since 9d55d96 ("Narrow loose comparisons by the values each side can loosely equal"), the true branch of $subject == <value> returns no specified types for a mixed subject when objects such as Stringable or GMP can loosely equal the value. That keeps the subject mixed, as intended, but it also skips what createSubjectTypes() does for the expression's parents: hasOffsetValue() on the array, the removal of false from array|false, and certainty for a possibly undefined variable.

The subject now still loses null when null cannot loosely equal the value: it becomes mixed~null (T of mixed~null for a template), so the offset and the variable count as set again. When null can equal the value (== 0, == ''), nothing is narrowed, as before.

The turbo-ext mirror in IdenticalNarrowingHelper.cpp gets the same change.

Verification:

  • tests/PHPStan/Analyser/nsrt/bug-15725.php and DefinedVariableRuleTest::testBug15725() fail without the fix and pass with it, with and without the extension loaded.
  • Existing expectations of mixed in loose-equality-narrowing.php and loose-equality-narrowing-php7.php become mixed~null.
  • Full test suite with and without the extension, the strict extension build, smoke.php and signature-parity.php, and byte-identical self-analysis output with the extension on and off.

When the values that can loosely equal the other side include objects,
a mixed subject stays mixed, but it still cannot be null when null does
not equal the value. Narrowing it to mixed~null again marks the offset
and the variable as set, which an early return without specified types
had stopped doing.

Closes phpstan/phpstan#15725

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d722-2ae4-761a-bc23-3426a9af3740
@ondrejmirtes
ondrejmirtes merged commit c8220b4 into 2.3.x Nov 14, 2026
899 of 906 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix-15725-loose-equal-mixed-not-null branch November 14, 2026 09:07
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.

Loose == with a mixed subject no longer narrows the offset or the variable as set (regression in 2.3.x-dev)

1 participant