Bug #80126 [Asn->Csd]: Covariant return types failing compilation

From: Date: Tue, 06 Oct 2020 14:34:17 +0000
Subject: Bug #80126 [Asn->Csd]: Covariant return types failing compilation
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229422@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80126&edit=1 ID: 80126 Updated by: nikic@php.net Reported by: terence-marks at zencontrol dot com Summary: Covariant return types failing compilation -Status: Assigned +Status: Closed Type: Bug Package: Compile Failure Operating System: Debian Buster PHP Version: 7.4.10 Assigned To: nikic Block user comment: N Private report: N New Comment: Automatic comment on behalf of nikita.ppv@gmail.com Revision: http://git.php.net/?p=php-src.git;a=commit;h=3b7c8bb973cb4b37ae5f034651d9e4cd4147729f Log: Fix bug #80126 Previous Comments: ------------------------------------------------------------------------ [2020-10-06 14:21:02] nikic@php.net @danack's example is a PHP 8 regressions, likely caused by a change in interface inheritance order. ------------------------------------------------------------------------ [2020-09-21 12:32:03] danack@php.net I don't think the extra interface is necessary to show the bug. This seems to fail also.. interface inty { public function intmethod(): inty; } abstract class AbstractClass implements inty { public function intmethod(): inty { return new static(); } } class Concrete extends AbstractClass { } class ExtendedConcrete extends Concrete { // fails public function intmethod(): ExtendedConcrete { return new static(); } // Works // public function intmethod(): Concrete { // return new static(); // } } ------------------------------------------------------------------------ [2020-09-21 01:42:27] terence-marks at zencontrol dot com 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 this bug report at https://bugs.php.net/bug.php?id=80126&edit=1

« previous php.bugs (#229422) next »