Skip to content

Turn a NAN float type into the 'NAN' string type without casting it - #6992

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
nan-float-to-string
Nov 7, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
nan-float-to-string

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

On PHP 8.5, (string) NAN warns "unexpected NAN value was coerced to string". ConstantFloatType::toString() cast its value directly, and the turbo mirror's zend_double_to_str() warns the same way. So analysing code where a float narrows to NAN made PHPStan report a phpstan.php error at ConstantFloatType.php:111.

It shows up in self-analysis of #6974: in BenchmarkReport::formatPercent(), $percent >= 10 || $percent === round($percent) ? ... : sprintf('%.1f', $percent) narrows $percent to a type that includes NAN, and make phpstan fails on PHP 8.5 and 8.6, with and without the turbo extension.

Both implementations now return the 'NAN' string type for a NAN before casting, the same string PHP produces. INF and -INF don't warn and are unchanged.

Tests: nsrt/nan-to-string.php fails on 2.3.x with the warning (PHPUnit's failOnWarning), both without and with the turbo extension, and passes with this change. The full paratest suite passes on PHP 8.5 without and with the extension, except OverridingMethodRuleConfigPhpTest::testTentativeReturnTypesInPhpVersionRange, which fails the same way in this environment without the change, and one DirectoryWalkerTest flake. turbo-ext smoke.php and signature-parity.php pass.

This isn't a performance change, so it has no timings.

PHP 8.5 warns "unexpected NAN value was coerced to string" when NAN is
cast to string, and zend_double_to_str() in the turbo mirror warns the
same way. That made PHPStan report a phpstan.php error while analysing
code where a float narrows to NAN, such as $x >= 10 || ... .

Amp-Thread-ID: https://ampcode.com/threads/T-01a1ad6c-dec2-72ff-bbe8-5b3b62746c94
@ondrejmirtes
ondrejmirtes merged commit 07b3911 into 2.3.x Nov 7, 2026
1039 of 1043 checks passed
@ondrejmirtes
ondrejmirtes deleted the nan-float-to-string branch November 7, 2026 09:05
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