Repository navigation
Conversation
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.
A float range that grows from one loop iteration to the next was not widened, so a loop accumulating a bounded float ended with the range its last analysed iteration reached. This PR widens a float range the way
MutatingScope::generalizeType()already widens an integer range: the bound that moved is opened.Impact
$sum += float<0, 1>from0.0float<0.0, 4.0>float<0.0, max>for ($x = 0.0; $x < 10; $x += float<0, 1>), insidefloat<0.0, 3.0>float<0.0, 9.999999999999998>float<10.0, max>$total += random_int(1, 6) / 20.0|float<0.5, 12.0>0.0|float<0.5, max>$budget -= float<0, 1>from10.0float<6.0, 10.0>float<min, 10.0>$amount *= float<1, 2>from100.0float<100.0, 1600.0>float<100.0, max>$sum = 0; $sum += int<0, 1>the same loop already ends withint<0, max>.greater.alwaysFalse,smaller.alwaysTrueanddeadCode.unreachablefalse positives, as in theforloop above.Cause
generalizeType()sorts the members of both iterations' types into buckets. Integer ranges get their own bucket, whose growing bound is opened. AFloatRangeType, new in 2.3.6, fell into the bucket of other types, which are unioned back unchanged. The loop handlers stop after a fixed number of iterations, so the range stayed at what those iterations reached.Fix
Float ranges get their own bucket with the same logic as integer ranges. The native
MutatingScopein turbo-ext gets the same change.A range that grows in both directions at once, like
$position += float<-1, 1>, is not opened. It keeps the range of both iterations, which is what integer ranges have done since #5176. With$position += int<-1, 1>the result isint<-4, 4>, and$position > 4is reported as always false the same way. I left that case for a separate change, because it applies to integers as well.Testing
tests/PHPStan/Analyser/nsrt/float-range-loop-generalize.phpcovers the loops in the table, the integer loops they are compared with, an array offset accumulated in nested loops, and a maximum that does not grow.NumberComparisonOperatorsConstantConditionRuleTest::testFloatRangeLoopGeneralize()analyses the same file and expects no errors. Without the fix, it reports the two comparisons above.