Bug #80126 [NEW]: Covariant return types failing compilation
| From: | terence-marks at zencontrol dot com | Date: | Mon, 21 Sep 2020 01:42:27 +0000 |
| Subject: | Bug #80126 [NEW]: Covariant return types failing compilation | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-229101@lists.php.net to get a copy of this message | ||
From: terence-marks at zencontrol dot com
Operating system: Debian Buster
PHP version: 7.4.10
Package: Compile Failure
Bug Type: Bug
Bug description:Covariant return types failing compilation
Description:
------------
Discovered an issue with narrowing return type using covariance.
I have a chain of inheritance (...ignore implementation details) from
which at the lowest level I attempt to narrow the return type for an
inherited method. When running the code PHP fails at compilation stating
the method is not compatible.
Now the weird part is it seems the covariance check passes if I remove
all interfaces from the lowest level (see example).
The attached test script should point you guys in the right direction.
Cheers,
Terence
Test script:
---------------
<?php
interface Poo
{
public function poo(): Poo;
}
abstract class AbstractPoo implements Poo
{
public function poo(): Poo
{
return new static();
}
}
class ConcretePoo extends AbstractPoo
{
}
interface B
{
}
// Fails if interface is present
class ExtendedConcretePoo extends ConcretePoo implements B
{
public function poo(): ExtendedConcretePoo
{
return new static();
}
}
// Does not fail if no interfaces present
//class ExtendedConcretePoo extends ConcretePoo
//{
// public function poo(): ExtendedConcretePoo
// {
// return new static();
// }
//}
$e = new ExtendedConcretePoo();
$r = $e->poo();
Expected result:
----------------
No compilation errors.
Actual result:
--------------
Declaration of ExtendedConcretePoo::poo(): ExtendedConcretePoo must be
compatible with AbstractPoo::poo()
--
Edit bug report at https://bugs.php.net/bug.php?id=80126&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80126&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80126&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80126&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80126&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80126&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80126&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80126&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80126&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80126&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80126&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80126&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80126&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80126&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80126&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80126&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80126&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80126&r=mysqlcfg