Repository navigation
Convert int and decimal-int-string to a finite float range in toFloat() and float arithmetic - #7102
Open
phpstan-bot wants to merge 2 commits into
Open
phpstan-bot wants to merge 2 commits into
phpstan-bot wants to merge 2 commits into
Conversation
…Float()` and float arithmetic - IntegerType::toFloat() returns float<(float) PHP_INT_MIN, (float) PHP_INT_MAX> instead of float, like IntegerRangeType::toFloat() already did for bounded ranges. This covers (float), floatval()/doubleval(), settype($x, 'float'|'double'), filter_var(FILTER_VALIDATE_FLOAT), round()/floor()/ceil() and the non-strict coerced argument type. - AccessoryDecimalIntegerStringType::toFloat() returns the same range when not inverted (a decimal-int-string always holds an int); the inverse still gives float. - NumberSet::integerToFloatInterval() bounds an unbounded integer side at the float closest to PHP_INT_MIN/PHP_INT_MAX instead of ±INF, so `$int + 0.0`, `$int * 1.0`, `$int / 2`, `$int += 0.0`, … are finite float ranges too, and int<0, max> + 0.0 no longer reaches INF. - Ported all three changes to the turbo-ext mirrors (IntegerType.cpp, AccessoryDecimalIntegerStringType.cpp, NumberSet.cpp). - Updated existing type assertions that now get more precise (finite bounds instead of `min`/`max`, `float` or ranges touching 0 for int divisors). - Probed but left alone: numeric-string/string casts (can be INF, e.g. "1e999"), fdiv() (has no range inference at all). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
staabm
requested changes
Nov 18, 2026
staabm
left a comment
Contributor
There was a problem hiding this comment.
What about
- int-range to float
- union-type to float
- bool to float
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
All three cases you asked about already give finite floats, so no source change was needed. I added tests that lock this in. Results:
Tests added:
|
Contributor
|
@zonuexe you are the float expert. do you have objections to this PR? :) |
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
PHPStan typed
(float) $intand(float) $decimalIntStringas plainfloat, so they could not be returned asfinite-float. An int always converts to a finite float, and so does a decimal-int-string. Now these conversions, and float arithmetic with an int operand ($int + 0.0, which the issue comment mentions), give a finite float range.Changes
src/Type/IntegerType.php:toFloat()returnsFloatRangeType::fromInterval((float) PHP_INT_MIN, (float) PHP_INT_MAX). This matches whatIntegerRangeType::toFloat()already did for bounded ranges. Everything built ontoFloat()benefits:(float),floatval()/doubleval(),settype(..., 'float'|'double'),filter_var(..., FILTER_VALIDATE_FLOAT),round()/floor()/ceil(), and the coerced argument type.src/Type/Accessory/AccessoryDecimalIntegerStringType.php:toFloat()returns the same range when not inverted. The inverted type (non-decimal-int-string) still returnsfloat, because e.g."1e999"becomes INF.src/Type/NumberSet.php:integerToFloatInterval()now maps an unbounded integer side to(float) PHP_INT_MIN/(float) PHP_INT_MAXinstead of ±INF. As a result,$int + 0.0,$int * 1.0,$int += 0.0,$int / 2andint<0, max> + 0.0are finite float ranges instead offloat/float<0.0, max>.IntegerType.cpp,AccessoryDecimalIntegerStringType.cpp,NumberSet.cpp. The extension builds,tests/smoke.phpprints ALL OK, and the full suite passes with the extension loaded.numeric-string/string→ float can be INF, so it is correctly not finite.fdiv()has no range inference at all; that is a separate feature.Root cause
Two places modelled "any int as a float" as unbounded:
IntegerType::toFloat()returnedFloatType.NumberSet::integerToFloatInterval()stretched an unbounded integer side to ±INF.Both forgot that ints are bounded by
PHP_INT_MIN/PHP_INT_MAX. The bounded case (IntegerRangeType::toFloat()) already handled this correctly. The decimal-int-string accessory had the same gap.Test
tests/PHPStan/Analyser/nsrt/bug-15767.php: the issue's reproducer plus casts,floatval/doubleval,settypewithfloatanddouble, arithmetic and compound assignment with float operands,int<0, max>,non-decimal-int-string(staysfloat), overflow ($int * 1e300staysfloat), andis_finite/is_nan.ReturnTypeRuleTest::testBug15767: no error for returning(float) $int,(float) $decimalIntString,$int + 0.0or asettyped int asfinite-float. The error is still reported for anon-decimal-int-string.float<0.0, max>→float<0.0, 9.223372036854776E+18>,(float|int)→(float<…>|int)for int division, and quotients with an int divisor no longer touching 0.strval(rand() * 0.1)is nownumeric-string&uppercase-string, because the float is finite.Fixes phpstan/phpstan#15767
🤖 Generated with Claude Code