Fix #11310: False Positive on Match Arm Comparison with Incremented Bounded Integer#4973
Open
phpstan-bot wants to merge 2 commits into2.1.xfrom
Open
Fix #11310: False Positive on Match Arm Comparison with Incremented Bounded Integer#4973phpstan-bot wants to merge 2 commits into2.1.xfrom
phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
- Pass pre-computed condition type and native type to MutatingScope::enterMatch() - Previously, enterMatch() re-evaluated the condition on the already-updated scope, causing post-increment ($i++) to use the incremented type instead of the original - Also fixes pre-increment (++$i) double-counting the increment in match context - New regression test in tests/PHPStan/Rules/Comparison/data/bug-11310.php Closes phpstan/phpstan#11310
Automated fix attempt 1 for CI failures.
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
When using
$i++(post-increment) as a match expression subject, PHPStan incorrectly used the post-incremented type for arm comparisons. For example, withint<0, max>parameter type,match($i++)would compare arms againstint<1, max>instead ofint<0, max>, causing false "always false" match arm comparison errors.Changes
src/Analyser/NodeScopeResolver.phpto capture the condition's native type ($condNativeType) before processing the expression, and pass both the PHPDoc type and native type toenterMatch()src/Analyser/MutatingScope.phpto accept optional$condTypeand$condNativeTypeparameters inenterMatch(), using them instead of re-evaluating the condition on the already-updated scopeRoot cause
In
NodeScopeResolver, the match expression condition was processed in two stages:$condType = $scope->getType($expr->cond)— captures the type before processing$this->processExprNode(...)— processes the condition, updating the scope (e.g., incrementing$i)$scope->enterMatch($expr)— called on the updated scopeInside
enterMatch(), the condition type was re-evaluated via$this->getType($cond)on the already-modified scope. For$i++, this meant the scope already reflected$iasint<1, max>, so the match subject type becameint<1, max>instead of the correctint<0, max>.The fix passes the pre-computed types (captured before processing) directly to
enterMatch(), ensuring the match subject uses the correct type regardless of side effects in the condition expression.Test
Added
tests/PHPStan/Rules/Comparison/data/bug-11310.phpwith three cases:$i++(post-increment): no false positive — match subject uses original value$i--(post-decrement): no false positive — match subject uses original value++$i(pre-increment): correctly reports always-false comparison withint<1, max>vs0Fixes phpstan/phpstan#11310