Repository navigation
Narrow the left side of ?? to non-null when a comparison type rules out the right side - #7096
Conversation
… out the right side - CoalesceHandler createTypesCallback: when the narrowing type rules the right side of `$left ?? $right` in (falsey context, e.g. `($x ?? '') !== ''`) or out (truthy context), the left side cannot be null, because a null left side would make the coalesce evaluate to the right side. Narrow the left side by `$type|null` in the falsey context and `$type` without null in the truthy one. - Same change in the legacy TypeSpecifier::createForExpr() coalesce unwrap, which is still reached (e.g. MethodSignatureRuleTest::testBug12073). - Mirror both changes in turbo-ext/src/CoalesceHandler.cpp and turbo-ext/src/TypeSpecifier.cpp. - In 2.3.5, `($x ?? '') == ''` happened to narrow through the loose comparison's `''|null` type. Since 2.3.6 that type is computed from the coalesce's non-nullable type, which lost the null removal. Strict `===`/`!==`, integer and bool right sides, `in_array()`, array offsets and nullsafe fetches on the left side never narrowed null away; they do now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VincentLanglet
left a comment
There was a problem hiding this comment.
Add non-regression test for phpstan/phpstan#9946
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I added the regression test for phpstan/phpstan#9946 that you asked for. It's committed locally as The test uses the code from the issue's playground link unchanged (
Both tests pass with the fix. With the old
|
VincentLanglet
left a comment
There was a problem hiding this comment.
Deduplicate the bug-9946 file (which will also solve the Name Collision Detector)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I removed the duplicate bug-9946 file and committed the change locally as
|
staabm
left a comment
There was a problem hiding this comment.
needs test to kill the mutant
https://github.com/phpstan/phpstan-src/actions/runs/44055115516/job/133945531369?pr=7096
| if (($bObj->valA?->value ?? 0) !== 0) { | ||
| assertType('Bug9946\A', $bObj->valA); | ||
| assertType('int<min, -1>|int<1, max>', $bObj->valA->value); | ||
| $test = $bObj->valA->value; | ||
| } |
There was a problem hiding this comment.
PHPStan reports this ?-> access is not necessary.
we should assert the types when people don't use ?-> in addition:
| if (($bObj->valA?->value ?? 0) !== 0) { | |
| assertType('Bug9946\A', $bObj->valA); | |
| assertType('int<min, -1>|int<1, max>', $bObj->valA->value); | |
| $test = $bObj->valA->value; | |
| } | |
| if (($bObj->valA?->value ?? 0) !== 0) { | |
| assertType('Bug9946\A', $bObj->valA); | |
| assertType('int<min, -1>|int<1, max>', $bObj->valA->value); | |
| $test = $bObj->valA->value; | |
| } | |
| // without null-safe operator | |
| if (($bObj->valA->value ?? 0) !== 0) { | |
| assertType('Bug9946\A', $bObj->valA); | |
| assertType('int<min, -1>|int<1, max>', $bObj->valA->value); | |
| $test = $bObj->valA->value; | |
| } |
There was a problem hiding this comment.
I added the case you suggested, without ?->, to tests/PHPStan/Analyser/nsrt/bug-9946.php. It failed: PHPStan inferred Bug9946\A|null for $bObj->valA, and the rule test reported "Cannot access property $value on Bug9946\A|null" twice. So I fixed that as well, in one local commit (c45842962). It isn't pushed.
The gap: ($bObj->valA->value ?? 0) !== 0 narrowed $bObj->valA->value, but not $bObj->valA. Without the fix, ($b->a->value ?? null) !== null doesn't narrow $b->a either. If the comparison rules out the right side of ??, the left side was set. So $bObj->valA can't be null, which is what isset($bObj->valA->value) already concludes.
The fix:
src/Analyser/ExprHandler/CoalesceHandler.php: increateTypesCallback, when the comparison type rules the right side in or out, the result now also includes the narrowing thatisset()applies to every link in the left chain (createIssetTruthyChainTypes).turbo-ext/src/CoalesceHandler.cpp: the same change in the native version. ItscreateTypesCallbacknow also captures$chainResults, like the PHP closure.
Checks:
bug-9946.phpandAccessPropertiesRuleTest::testBug9946failed before the fix and pass after it, with and without the extension.bug-15748.phpstill passes.make testspasses (23863 tests, 116 skipped). The full suite also passes with the turbo extension loaded.- The extension builds with the strict warning flags, and
tests/smoke.phpprints ALL OK. make phpstanreports no errors.
One case is still not narrowed: a bare truthy check like if ($b->a->value ?? false) leaves $b->a as A|null. It goes through a different callback, so I left it out of this PR.
I couldn't see the details of the mutant from the earlier CI run. If the mutation testing job still reports it after this push, the log will show which line needs another test.
Matches the turbo-ext mirror, which already starts from the original type, and drops the `$exprType ?? $type` fallbacks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kills the mutants of the true()/false() branch choice in CoalesceHandler and TypeSpecifier: the negated-true context (`!== true`), legacy TypeSpecifier::create() calls from type-specifying extensions, and create() called directly on a coalesce with a nullable type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I added tests that kill the escaped mutants and made one small cleanup in Why mutants survived: the code now narrows the left side of
Commits:
Mutants checked: each was applied on its own, and each made at least one new test fail.
|
Summary
if (($x ?? '') == '') return;no longer narrowed?string $xtonon-empty-stringin 2.3.6. When a comparison rules out the right side of??, PHPStan now concludes that the left side is not null.Changes
src/Analyser/ExprHandler/CoalesceHandler.php(createTypesCallback): when the narrowing type rules the right side in (falsey context) or out (truthy context), the left side is narrowed byTypeCombinator::addNull($type)(falsey) orTypeCombinator::removeNull($type)(truthy), instead of by$typealone.src/Analyser/TypeSpecifier.php(createForExpr): same change in the legacy coalesce unwrap, which is still reachable (e.g.MethodSignatureRuleTest::testBug12073).turbo-ext/src/CoalesceHandler.cpp,turbo-ext/src/TypeSpecifier.cpp: native mirrors of both changes. The extension builds with strict warnings,tests/smoke.phpprints ALL OK, and the regression test passes with the extension loaded.Root cause
For
$left ?? $rightnarrowed by a typeT:$rightis always inTand the result is notT, the left side was not null. A null left side would have produced$right.$rightis never inTand the result isT, the same holds.The handler unwrapped the coalesce to its left side but only applied
T, sonullsurvived on the left. In 2.3.5 the reported loose case (== '') worked by accident: the loose comparison narrowed by''|null, which also removed null. Since the loose comparison rework in 2.3.6, that type comes from the coalesce's own non-nullable type (string), so it is just''and null was no longer removed. The strict forms (=== '',!== '') were broken in 2.3.5 as well.Test
tests/PHPStan/Analyser/nsrt/bug-15748.php:!=,===,!==, and integer== 0/=== 0.($a['k'] ?? '') !== ''($b ?? false) !== false!in_array($x ?? '', [...], true)($o?->bar ?? '') !== ''($x ?? 'a') === null(now*NEVER*)($x ?? '') !== 'abc'staysstring|null.All of these fail without the fix. Already correct before the fix:
($i ?? 0) > 0,strlen($x ?? '') > 0, and chained$x ?? $y ?? ''.Fixes phpstan/phpstan#15748
Fixes phpstan/phpstan#9946