Skip to content

Widen constant floats that change in a loop to a float range like constant integers - #7085

Open
zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:generalize-constant-floats
Open

zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:generalize-constant-floats

Conversation

@zonuexe

@zonuexe zonuexe commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

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 way MutatingScope::generalizeType() already widens constant integers: the bound that moved is opened, and the result is a float range.

Impact

/** @param non-negative-float $x */
function takesNonNegativeFloat(float $x): void {}

/** @param list<string> $items */
function f(array $items): void
{
	$total = 0.0;
	foreach ($items as $item) {
		$total += 1.5;
	}
	// Parameter #1 $x of function takesNonNegativeFloat expects float<0.0, max>, float given.
	takesNonNegativeFloat($total);
}
Loop Before After
$total += 1.5 from 0.0 float 0.0|1.5|float<3.0, max>
$count += 2 from 0, for comparison 0|2|int<4, max> unchanged
for ($x = 0.0; …; $x += 0.25), inside float 0.0|float<0.25, max>
$budget -= 0.5 from 10.0 float 9.5|10.0|float<min, 9.0>
$size /= 2 from 1.0 float 0.5|1.0|float<min, 0.25>
$value += 1.0 from NAN or 0.0 float 0.0|1.0|NAN|float<2.0, max>
$sign = -$sign from 1.0 float float<min, max>
  • float includes 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.
  • The integer version of the same loop is not reported.

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

  • Constant floats get their own block with the logic of the constant integers. The native MutatingScope in turbo-ext gets the same change.
  • NAN has no place in the order. It is set aside and added back to the result, so the minimum and maximum come from the other values.
  • When one side has no value other than NAN, the floats are unioned, as constant integers are when one side has none.

Changed expectation

In StrictComparisonOfDifferentTypesRuleTest, ConstantAssignOperatorInWhile divides $i = 10.0 by 5 in a loop. $i === 1000000.0 is 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 $i never 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.php covers 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.
  • With turbo loaded and the PHP twin reverted, the loops stay fixed, so the native change is exercised. The raw analysis output is identical with the extension on and off.

This is independent of #7084, which widens float ranges in the same function. The two branches only conflict in the use const lines, and both tests pass on the merged tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant