Resolve array offset access on never to never and treat never operands of ===/!== as undecided#5906
Open
phpstan-bot wants to merge 1 commit into
Open
Conversation
…perands of `===`/`!==` as undecided - ArrayDimFetchHandler::resolveType() now short-circuits when the offset-accessible type is NeverType. Because never is a subtype of everything (including ArrayAccess), the fetch was otherwise resolved through offsetGet() and produced an *ERROR* type instead of never. - InitializerExprTypeResolver::resolveIdenticalType() returns a non-constant BooleanType (instead of ConstantBooleanType(false)) when either operand is never. A never-typed operand has no value to compare, so the comparison is undecided. This mirrors how never already behaves as a boolean condition (if/&&/||) and stops StrictComparison / ImpossibleCheck rules from piling always-true/false errors onto already-unreachable code. - Together these let an impossible assertion such as `assert($array[1] === null)` collapse the array to never without emitting a cascade of `*NEVER* === ...` comparison errors on the following statements. - Updated the last-match-arm rule test (the `*NEVER* === 'ccc'` reports are now suppressed) and bug-9307 (`array<*ERROR*>` is now correctly inferred as `array<int, Bug9307\Item>`). - Probed siblings: `==`/`!=` already resolve to a non-constant boolean via NeverType::looseCompare(); `<=>`/`<`/`>` yield never without an always-true/false report, so no change was needed there.
b5b4dbe to
661cf30
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.
Summary
An impossible identical comparison inside
assert()over a constant array — e.g.assert($array[1] === null)where$array[1]is0— collapses the whole array tonever(which is correct). The bug was that the following statements then produced a cascade of spuriousStrict comparison using === between *NEVER* and null will always evaluate to false.errors, and that reading a further offset of the now-neverarray produced an*ERROR*type instead ofnever.This was the regression behind the revert of d7ba1e3 ("More precise array-item types in loops"). This change fixes the root cause so the precision improvement no longer produces the cascade.
Changes
src/Analyser/ExprHandler/ArrayDimFetchHandler.php— short-circuitresolveType()when the offset-accessible type isNeverType, returningnever. Previously, becauseneveris a subtype of everything (includingArrayAccess), the fetch was routed throughoffsetGet()and produced*ERROR*.src/Reflection/InitializerExprTypeResolver.php—resolveIdenticalType()now returns a non-constantBooleanType(instead ofConstantBooleanType(false)) when either operand isnever, so===/!==no longer reports always-true/false on already-unreachable code.tests/PHPStan/Analyser/data/bug-9307.php— the loop case now correctly infersarray<int, Bug9307\Item>instead ofarray<*ERROR*>(the inline comment already predicted this).tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php— the last-match-arm expectations drop the two*NEVER* === 'ccc'reports, plus a newtestBug14281.tests/PHPStan/Analyser/nsrt/bug-14281.phpandtests/PHPStan/Rules/Comparison/data/bug-14281.php.Root cause
Two independent spots mishandled
never:Offset read on
never.ArrayDimFetchHandlerchecks(new ObjectType(ArrayAccess::class))->isSuperTypeOf($type)->yes()to decide whether to calloffsetGet().neversatisfies that test (it is a subtype of every type), so an offset read on aneverarray was treated as anArrayAccess::offsetGet()call and yielded*ERROR*.NeverType::getOffsetValueType()already returnsnever; the handler just never reached it.Identical comparison with a
neveroperand.resolveIdenticalType()returned a constantfalse, which theStrictComparisonOfDifferentTypesRule/ImpossibleCheckType*rules report as "always false". Aneveroperand denotes unreachable code that carries no comparable value, so the result should be undecided — consistent with howneveralready behaves as a boolean condition (if,&&,||) and withNeverType::looseCompare()for==/!=.Test
nsrt/bug-14281.phpasserts that afterassert($array[1] === null)the array is*NEVER*, that$array[2]is*NEVER*(not*ERROR*), and that$i === null/$i !== nullon anevervariable are inferred asbool. It fails without the fix (Actual: *ERROR*and constant-boolean results).Rules/Comparison/data/bug-14281.php+testBug14281confirm the rule only reports the genuinely-impossible first comparisons (null === null,0 === null,int !== int) and no longer reports the unreachable*NEVER* === null/*NEVER* !== nullfollow-ups.==/!=already produce a non-constant boolean viaNeverType::looseCompare();<=>/</>produceneverwithout an always-true/false report. No change needed for those.Fixes phpstan/phpstan#14281