Skip to content

[RFC] Warn on implicit float to int conversions. #6661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 61 commits into from
May 31, 2021

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Feb 3, 2021

Relatively straight forward implementation, might need to check OPcache.

RFC is located here: https://wiki.php.net/rfc/implicit-float-int-deprecate.

  • JIT Fixes
  • 32bit fixes

@Girgias Girgias force-pushed the float-to-int-warn branch 2 times, most recently from 63f887f to 32e94f0 Compare February 20, 2021 11:59
@Girgias Girgias force-pushed the float-to-int-warn branch from 32e94f0 to d87efe5 Compare March 20, 2021 04:45
Copy link
Contributor

@TysonAndre TysonAndre left a comment

Choose a reason for hiding this comment

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

I don't see any issues with this pr at a glance, only nits. I'm not familiar enough with opcache/jit to know if anything else needs to be changed.

I'd personally consider this an improvement over silently allowing the loss of precision, especially for array keys

Also, I think this will work with negative zero but it'd be useful to have at least one test case in case the implementation ever changes or the JIT ever unintentionally differs on some platforms. https://en.wikipedia.org/wiki/Signed_zero#Comparisons mentions

According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of C and Java.

(i.e. since PHP's === uses C's == for floats I don't expect issues)

php > $negativeZero = -0.0;
php > var_dump($negativeZero);
float(-0)
php > var_dump($negativeZero === (float)(int)$negativeZero);
bool(true)
php > var_dump($negativeZero === 0.0);
bool(true)

@Girgias Girgias force-pushed the float-to-int-warn branch from d87efe5 to 4d1be39 Compare April 5, 2021 05:36
@Girgias Girgias force-pushed the float-to-int-warn branch from 4d1be39 to a92b3e5 Compare April 13, 2021 12:37
@Girgias Girgias force-pushed the float-to-int-warn branch 2 times, most recently from 7278201 to adfc720 Compare April 19, 2021 02:41
@Girgias Girgias force-pushed the float-to-int-warn branch 3 times, most recently from dd057df to 51a75e5 Compare April 20, 2021 13:33
@Girgias Girgias force-pushed the float-to-int-warn branch from e719449 to 09a8044 Compare May 31, 2021 11:22
@Girgias Girgias merged commit b6958bb into php:master May 31, 2021
@Girgias Girgias deleted the float-to-int-warn branch May 31, 2021 14:48
@Ayesh
Copy link
Member

Ayesh commented Jun 3, 2021

Thank you @Girgias for this, and congratulations on the unanimous RFC vote yes.

Really trying not to bike-shed, but do you think using incompatible instead of non-compatible would be any better? Google ngram shows incompatible is used more than non-compatible, and perhaps we could improve the error message a little bit more?

We already use incompatible in error messages, but I don't think we do for other error messages apart from this one.

If you think it's a good idea, I would gladly send a PR through. I apologize if the choice of non-compatible is deliberate; English is my secondary language and incompatible felt a bit more natural, hence this comment.

Thank you.

@Girgias
Copy link
Member Author

Girgias commented Jun 3, 2021

Feel free to send a PR, I didn't think a lot about the deprecation message to be honest (and it's been a while). :-)

Ayesh added a commit to Ayesh/php-src that referenced this pull request Jun 3, 2021
Updates the deprecation message for implicit incompatible float to int conversion from:

```
Implicit conversion from non-compatible
```

to

```
Implicit conversion from incompatible
```

Related: php#6661
@Ayesh
Copy link
Member

Ayesh commented Jun 3, 2021

Thanks a lot @Girgias - I just made #7095 with the changes.

Ayesh added a commit to Ayesh/php-src that referenced this pull request Jun 6, 2021
Updates the deprecation message for implicit incompatible float to int conversion from:

```
Implicit conversion from non-compatible float %.*H to int in %s on line %d
```

to

```
Implicit conversion from float %.*H to int loses precision in %s on line %d
```

Related: php#6661
Ayesh added a commit to Ayesh/php-src that referenced this pull request Jun 6, 2021
Updates the deprecation message for implicit incompatible float to int conversion from:

```
Implicit conversion from non-compatible float %.*H to int in %s on line %d
```

to

```
Implicit conversion from float %.*H to int loses precision in %s on line %d
```

Related: php#6661
nikic pushed a commit that referenced this pull request Jun 7, 2021
Updates the deprecation message for implicit incompatible float to int conversion from:

```
Implicit conversion from non-compatible float %.*H to int in %s on line %d
```

to

```
Implicit conversion from float %.*H to int loses precision in %s on line %d
```

Related: #6661
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants