Req #80697 [NEW]: Return type declaration int allows floats to be returned
| From: | david dot m dot wakelin at gmail dot com | Date: | Tue, 02 Feb 2021 12:38:21 +0000 |
| Subject: | Req #80697 [NEW]: Return type declaration int allows floats to be returned | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-231884@lists.php.net to get a copy of this message | ||
From: david dot m dot wakelin at gmail dot com
Operating system: Ubuntu 18.04.5 LTS
PHP version: 8.0.1
Package: *Programming Data Structures
Bug Type: Feature/Change Request
Bug description:Return type declaration int allows floats to be returned
Description:
------------
When specifying int as a return type declaration, a type of float is
allowed to be returned, which gets cast to an int.
All other type coercions preserve the meaning of the returned data,
whereas coercing float to int will lose information after the decimal,
therefore not returning the true meaning.
This is not a good feature in my opinion and has been the cause of a
couple of uncaught bugs for me.
I suggest that a float to int should be non-coercible in return types
and should raise a TypeError as standard and not just when using
strict_types.
Test script:
---------------
class Test
{
public static function getInt(): int
{
return 5.67;
}
}
Test::getInt(); // 5 but would like to see a TypeError
Expected result:
----------------
getInt returns a 5, but I think it should raise a TypeError
--
Edit bug report at https://bugs.php.net/bug.php?id=80697&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80697&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80697&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80697&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80697&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80697&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80697&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80697&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80697&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80697&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80697&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80697&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80697&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80697&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80697&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80697&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80697&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80697&r=mysqlcfg