Bug #79511 [Com]: Return is silently ignored with yield even if yield is never reached

From: Date: Thu, 23 Apr 2020 08:20:55 +0000
Subject: Bug #79511 [Com]: 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-226728@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 Comment by: michael dot vorisek at email dot cz Reported by: michael dot vorisek at email dot cz Summary: Return is silently ignored with yield even if yield is never reached Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: any PHP Version: 7.4.5 Assigned To: cmb Block user comment: N Private report: N New Comment: But the 2nd example does not call yield at all - if (false) {} pattern is used a lot to comment code to keep it refactorable - why is this allowed and where is this behaviour defined? Previous Comments: ------------------------------------------------------------------------ [2020-04-23 07:16:41] cmb@php.net 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>. ------------------------------------------------------------------------ [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 (#226728) next »