Bug #79511 [Opn->Nab]: Return is silently ignored with yield even if yield is never reached

From: Date: Thu, 23 Apr 2020 07:16:41 +0000
Subject: Bug #79511 [Opn->Nab]: Return is silently ignored with yield even if yield is never reached
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226724@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79511&edit=1 ID: 79511 Updated by: cmb@php.net Reported by: michael dot vorisek at email dot cz Summary: Return is silently ignored with yield even if yield is never reached -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: any PHP Version: 7.4.5 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: No, the return is not ignored; it just has special semantics in generator functions; see <https://3v4l.org/rCAX4> and <https://github.com/php/php-langspec/blob/master/spec/14-classes.md#class-generator>. Previous Comments: ------------------------------------------------------------------------ [2020-04-23 00:38:19] michael dot vorisek at email dot cz Description: ------------ See test script Test script: --------------- function x(): iterable { yield 'a'; return ['b']; // silently ignored! } function y(): iterable { if (false) { yield 'c'; } return ['d']; // not returned at all - yield above needs to be commented out even if the condition is never matched! } foreach (x() as $v) {echo $v."\n";} echo "\n"; foreach (y() as $v) {echo $v."\n";} Expected result: ---------------- a b d Actual result: -------------- a ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79511&edit=1

« previous php.bugs (#226724) next »