Resolve array offset access on never to never and treat never operands of ===/!== as undecided#5906
Conversation
b5b4dbe to
661cf30
Compare
staabm
left a comment
There was a problem hiding this comment.
resolve conflicts and rebase
| [ | ||
| "Strict comparison using === between *NEVER* and 'ccc' will always evaluate to false.", | ||
| 38, | ||
| ], |
There was a problem hiding this comment.
we need this errors to stay
661cf30 to
cae1545
Compare
A case whose subject was already exhausted to never sits on unreachable code. A never operand keeps the loose comparison undecided (see phpstan#5906, which established never operands of ===/== as undecided precisely to stop piling always-true/false errors onto unreachable code), so the trailing case is deliberately not reported. Clarify the misleading fixture and add an explicit no-error note to the test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…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.
cae1545 to
d8a8abe
Compare
| // A never-typed operand has no value to compare, so the result is | ||
| // undecided. This mirrors how never behaves as a boolean condition and | ||
| // keeps always-true/false rules from piling onto already-unreachable code. | ||
| return new TypeResult(new BooleanType(), []); |
There was a problem hiding this comment.
Done. Here's a summary of how I addressed the review.
What the reviewer asked
- "revert this part" — the
InitializerExprTypeResolver::resolveIdenticalType()change that returned a non-constantBooleanTypeforneveroperands. - "we need this errors to stay" (earlier comment) — the
*NEVER* === 'ccc'always-false reports removed from the match-arm test.
Changes made
src/Reflection/InitializerExprTypeResolver.php— revertedresolveIdenticalType()back toreturn new TypeResult(new ConstantBooleanType(false), [])forneveroperands. TheArrayDimFetchHandlerfix (offset access onnever→neverinstead of*ERROR*) is untouched, since the review only targeted the resolver.StrictComparisonOfDifferentTypesRuleTest.php— restored the two*NEVER* === 'ccc'(line 38) error expectations indataLastMatchArm, and updatedtestBug14281to expect the always-false/true reports that reappear on the unreachable comparisons.tests/PHPStan/Rules/Comparison/data/bug-14281.php— corrected the now-inaccurate comments (the comparisons on the collapsed-to-neverarray are reported again) instead of leaving the false "must not be reported" claim.tests/PHPStan/Analyser/nsrt/bug-14281.php— updatedneverVariableexpectations fromboolto the actualfalse/trueconstant results; the array-offset assertions (the remaining real fix) are unchanged.
I empirically confirmed the reverted behavior via the failing/passing tests rather than guessing line numbers.
Verification
testBug14281,testLastMatchArm,bug-14281nsrt, andbug-9307— all pass.- Full
tests/PHPStan/Rules/Comparison/(426) andNodeScopeResolverTest(1679) — all green. make phpstan— no errors.
Committed as a single logical change and pushed to create-pull-request/patch-20i99q4.
One thing worth flagging for the reviewer: with the resolver reverted but the ArrayDimFetchHandler fix kept, offset reads on a collapsed-to-never array now produce genuine never (correct), which the strict-comparison rule then reports as always-false on the deeply-unreachable follow-up statements. That's why testBug14281 gained the extra *NEVER* === null expectations — the reproducer now emits more reports on unreachable code than before, which is the opposite of the issue's "no errors" goal. If that cascade is undesirable, the two changes may need to move together rather than splitting them.
Revert resolveIdenticalType() to return ConstantBooleanType(false) when either operand is never, as requested in review. The always-false/true reports on never operands (including *NEVER* === 'ccc' in the last match arm) are kept. The array offset access on never -> never fix in ArrayDimFetchHandler is unaffected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A case whose subject was already exhausted to never sits on unreachable code. A never operand keeps the loose comparison undecided (see phpstan#5906, which established never operands of ===/== as undecided precisely to stop piling always-true/false errors onto unreachable code), so the trailing case is deliberately not reported. Clarify the misleading fixture and add an explicit no-error note to the test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A case whose subject was already exhausted to never sits on unreachable code. A never operand keeps the loose comparison undecided (see phpstan#5906, which established never operands of ===/== as undecided precisely to stop piling always-true/false errors onto unreachable code), so the trailing case is deliberately not reported. Clarify the misleading fixture and add an explicit no-error note to the test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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