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 loop that changes a float by constants generalized it to plain
float, while the same loop over integers gives a range. This PR widens constant floats the wayMutatingScope::generalizeType()already widens constant integers: the bound that moved is opened, and the result is a float range.Impact
$total += 1.5from0.0float0.0|1.5|float<3.0, max>$count += 2from0, for comparison0|2|int<4, max>for ($x = 0.0; …; $x += 0.25), insidefloat0.0|float<0.25, max>$budget -= 0.5from10.0float9.5|10.0|float<min, 9.0>$size /= 2from1.0float0.5|1.0|float<min, 0.25>$value += 1.0fromNANor0.0float0.0|1.0|NAN|float<2.0, max>$sign = -$signfrom1.0floatfloat<min, max>floatincludes NAN and every negative value, so passing the result to a parameter typed with one of the float range types from 2.3.6 was reported, as above.Cause
generalizeType()handled constant floats in the same loop as constant booleans and constant strings: two different sets of constants are generalized to their general type. Constant integers have their own block, which compares the minimum and maximum of both iterations and opens the bound that moved. Float ranges did not exist when the shared loop was written.Fix
MutatingScopein turbo-ext gets the same change.Changed expectation
In
StrictComparisonOfDifferentTypesRuleTest,ConstantAssignOperatorInWhiledivides$i = 10.0by 5 in a loop.$i === 1000000.0is now reported as always false, inside and after the loop. That is correct: the values are 10, 2, 0.4, 0.08 and so on, so$inever reaches 1000000.0. The comparison with an integer counter next to it, which can reach 1000000, is still not reported.Testing
tests/PHPStan/Analyser/nsrt/float-constant-loop-generalize.phpcovers the loops in the table and the integer loops they are compared with.CallToFunctionParametersRuleTest::testFloatConstantLoopGeneralize()analyses the same file and expects no errors. Without the fix, it reports the call above.This is independent of #7084, which widens float ranges in the same function. The two branches only conflict in the
use constlines, and both tests pass on the merged tree.