Skip to content

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
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-2rexbio
Open

phpstan-bot wants to merge 2 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-2rexbio

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

PHPStan typed (float) $int and (float) $decimalIntString as plain float, so they could not be returned as finite-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() returns FloatRangeType::fromInterval((float) PHP_INT_MIN, (float) PHP_INT_MAX). This matches what IntegerRangeType::toFloat() already did for bounded ranges. Everything built on toFloat() 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 returns float, 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_MAX instead of ±INF. As a result, $int + 0.0, $int * 1.0, $int += 0.0, $int / 2 and int<0, max> + 0.0 are finite float ranges instead of float / float<0.0, max>.
  • turbo-ext mirrors ported: IntegerType.cpp, AccessoryDecimalIntegerStringType.cpp, NumberSet.cpp. The extension builds, tests/smoke.php prints ALL OK, and the full suite passes with the extension loaded.
  • Probed and left unchanged:
    • 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() returned FloatType.
  • 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, settype with float and double, arithmetic and compound assignment with float operands, int<0, max>, non-decimal-int-string (stays float), overflow ($int * 1e300 stays float), and is_finite/is_nan.
  • ReturnTypeRuleTest::testBug15767: no error for returning (float) $int, (float) $decimalIntString, $int + 0.0 or a settyped int as finite-float. The error is still reported for a non-decimal-int-string.
  • Updated existing nsrt assertions that now get more precise types. Examples: 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 now numeric-string&uppercase-string, because the float is finite.

Fixes phpstan/phpstan#15767

🤖 Generated with Claude Code

…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 staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

  • int-range to float
  • union-type to float
  • bool to float

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@phpstan-bot

Copy link
Copy Markdown
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:

  • Int ranges (this already worked before this PR): IntegerRangeType::toFloat() keeps any finite bounds and caps an open side at the float closest to PHP_INT_MIN/PHP_INT_MAX.
    • int<0, 10> → float<0.0, 10.0>
    • int<0, max> → float<0.0, 9.223372036854776E+18>
    • int<min, 5> → float<-9.223372036854776E+18, 5.0>
    • 1|2|3 → 1.0|2.0|3.0
    • int<0, 10> + 0.0 → float<0.0, 10.0>
  • Bool (also already worked): (float) $bool → 0.0|1.0, and (float) true → 1.0.
  • Unions: each member is converted on its own and the results are combined. Before this PR, the int member became plain float, so the whole union did too. Now:
    • int|bool, int|decimal-int-string and ?int → float<-9.223372036854776E+18, 9.223372036854776E+18>, both through (float) and floatval().
    • int|string stays float, because a string like "1e999" becomes INF.
    • int|float stays float, because the float member can be INF or NAN.

Tests added:

  • tests/PHPStan/Analyser/nsrt/bug-15767.php: a new review() function asserts every case above.
  • ReturnTypeRuleTest::testBug15767: returning (float) $x as finite-float gives no error for int<0, max>, bool, or int|bool|decimal-int-string|null. It still gives an error for int|string.

make tests passed (23903 tests, 116 skipped) and make phpstan reported no errors. Everything is in one commit (56235b6dc), not pushed yet.

@staabm

staabm commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

@zonuexe you are the float expert. do you have objections to this PR? :)

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.

Casting an int (or a decimal-int-string) as float yields a finite-float

2 participants