Skip to content

Widen a float range that grows in a loop like an integer range - #7084

Open
zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:generalize-float-range
Open

zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:generalize-float-range

Conversation

@zonuexe

@zonuexe zonuexe commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

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

/** @param list<float<0, 1>> $weights */
function total(array $weights): float
{
	$sum = 0.0;
	foreach ($weights as $weight) {
		$sum += $weight;
	}
	// float<0.0, 4.0>, so: Comparison operation ">" between float<0.0, 4.0> and 4.0 is always false.
	if ($sum > 4.0) {
		echo "heavy\n";
	}

	return $sum;
}
Loop Before After
$sum += float<0, 1> from 0.0 float<0.0, 4.0> float<0.0, max>
for ($x = 0.0; $x < 10; $x += float<0, 1>), inside float<0.0, 3.0> float<0.0, 9.999999999999998>
same, after the loop unreachable float<10.0, max>
$total += random_int(1, 6) / 2 0.0|float<0.5, 12.0> 0.0|float<0.5, max>
$budget -= float<0, 1> from 10.0 float<6.0, 10.0> float<min, 10.0>
$amount *= float<1, 2> from 100.0 float<100.0, 1600.0> float<100.0, max>
  • With $sum = 0; $sum += int<0, 1> the same loop already ends with int<0, max>.
  • The wrong ranges caused greater.alwaysFalse, smaller.alwaysTrue and deadCode.unreachable false positives, as in the for loop above.

Cause

generalizeType() sorts the members of both iterations' types into buckets. Integer ranges get their own bucket, whose growing bound is opened. A FloatRangeType, 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 MutatingScope in 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 is int<-4, 4>, and $position > 4 is 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.php covers 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.
  • 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.

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