Bug #81330 [Nab]: Generator does not allow return null with implicit yield

From: Date: Wed, 04 Aug 2021 11:39:32 +0000
Subject: Bug #81330 [Nab]: Generator does not allow return null with implicit yield
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235589@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81330&edit=1 ID: 81330 Updated by: cmb@php.net Reported by: greedy dot ivan at gmail dot com Summary: Generator does not allow return null with implicit yield Status: Not a bug Type: Bug Package: *General Issues Operating System: All PHP Version: 8.0.9 Assigned To: cmb Block user comment: N Private report: N New Comment: If the generator function does not necessarily return a Generator, you shouldn't declare it as such. Previous Comments: ------------------------------------------------------------------------ [2021-08-04 11:39:32] cmb@php.net If the generator function does not necessarily return a Generator, you shouldn't declare it as such. ------------------------------------------------------------------------ [2021-08-04 11:29:25] greedy dot ivan at gmail dot com Description: ------------ If there is not explicit yield instruction in Generator, it will throw TypeError message when null is returned. https://3v4l.org/EpIjA Test script: --------------- <?php function foo($flag = false): \Generator { if ($flag) { return null; } yield from [1, 2]; } function bar($flag = false): \Generator { if ($flag) { return null; } return internal(); } function internal(): \Generator { yield from [1, 2]; } foreach (foo() as $v){} foreach (foo(true) as $v){} foreach (bar() as $v){} foreach (bar(true) as $v){} Expected result: ---------------- No error Actual result: -------------- Fatal error: Uncaught TypeError: bar(): Return value must be of type Generator, null returned in /in/EpIjA:15 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81330&edit=1

« previous php.bugs (#235589) next »