Fix phpstan/phpstan#12163: non-negative-int evaluated to int#5176
Open
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#12163: non-negative-int evaluated to int#5176phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Conversation
- Fixed overly aggressive type generalization in MutatingScope::generalizeType() for constant integers when values expand in both directions across loop iterations - Instead of widening to plain `int`, now computes actual observed bounds, allowing the next iteration to correctly determine stable vs growing bounds - New regression test in tests/PHPStan/Analyser/nsrt/bug-12163.php Closes phpstan/phpstan#12163
Contributor
|
@phpstan-bot the fix and test applies to lines 4115-4127 in MutatingScope. for symmetry we need the same fix, covered by a test, in line 4207 of MutatingScope |
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 a variable was assigned
0and then modified inside a for loop (incremented or reset to0), PHPStan incorrectly generalized its type tointinstead of preservingint<0, max>(non-negative-int). This caused false positive errors like "expects int<0, max>, int given".Changes
MutatingScope::generalizeType()insrc/Analyser/MutatingScope.phpgotGreater && gotSmaller)IntegerType(plainint), the fix computes the actual observed min/max bounds from both iterations usingIntegerRangeType::fromInterval($newMin, $newMax)tests/PHPStan/Analyser/nsrt/bug-12163.phpRoot cause
In
generalizeType(), constant integer values from consecutive loop iterations are compared. If values in the current iteration are both greater AND smaller than the previous iteration's range, the code assumed both bounds were unstable and widened toint.In the reported case,
$columnIndexstarted at0, was incremented ($columnIndex++) in one branch and reset to0in another. After the first loop iteration, the type was1(only the increment path was reachable). After the second iteration (merged with init scope), it became0|1|2. The generalization saw1→0|1|2— values grew in both directions — and concludedint.The fix computes the actual combined bounds (
int<0, 2>) instead. On the next generalization iteration, this becomes anIntegerRangeType, which the integer range generalization logic handles correctly — it sees only upward growth and producesint<0, max>. For truly unbounded cases (both directions growing indefinitely), the integer range generalization in the subsequent iteration still correctly producesint.Test
Added
tests/PHPStan/Analyser/nsrt/bug-12163.phpwhich reproduces the original issue: a for loop with two index variables where one is incremented or reset to 0. Both$rowIndexand$columnIndexare asserted to beint<0, max>.Fixes phpstan/phpstan#12163