Bug #77587 [NEW]: Inconsistent behaviour when returning generators

From: Date: Fri, 08 Feb 2019 12:03:57 +0000
Subject: Bug #77587 [NEW]: Inconsistent behaviour when returning generators
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219438@lists.php.net to get a copy of this message
From: adamnicholson10 at gmail dot com Operating system: Linux PHP version: Irrelevant Package: *General Issues Bug Type: Bug Bug description:Inconsistent behaviour when returning generators Description: ------------ There appears to be inconsistent behaviour when returning generators. Given the function foo() returns a generator, when you consume that generator in a new function which also returns a generator, you would expect return foo() to have the same behaviour as looping over the values of foo() and performing a yield on each value; however this doesn't appear to be the case if the function you are consuming foo() contains other yield statements. It looks like PHP is getting confused by having both yield statements, and return $generator statements, in the same function body. This appears to affect all PHP versions tested which support generators POC (code below): https://3v4l.org/e86mK Test script: --------------- <?php function generator() { foreach ([1, 2, 3, 4, 5] as $i) { yield $i; } } function A(): \Generator { return generator(); } function B(): \Generator { if (false) { yield 7; return; } return generator(); } function C(): \Generator { if (false) { yield 7; return; } foreach (generator() as $i) { yield $i; } } echo "-A\n"; foreach (A() as $i) { echo "{$i}\n"; } echo "-B\n"; foreach (B() as $i) { echo "{$i}\n"; } echo "-C\n"; foreach (C() as $i) { echo "{$i}\n"; } echo "Done\n"; Expected result: ---------------- -A 1 2 3 4 5 -B 1 2 3 4 5 -C 1 2 3 4 5 Done Actual result: -------------- -A 1 2 3 4 5 -B -C 1 2 3 4 5 Done -- Edit bug report at https://bugs.php.net/bug.php?id=77587&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77587&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77587&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77587&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=77587&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=77587&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=77587&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=77587&r=needscript Try newer version: https://bugs.php.net/fix.php?id=77587&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=77587&r=support Expected behavior: https://bugs.php.net/fix.php?id=77587&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=77587&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=77587&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=77587&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77587&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=77587&r=dst IIS Stability: https://bugs.php.net/fix.php?id=77587&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=77587&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=77587&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=77587&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=77587&r=mysqlcfg

« previous php.bugs (#219438) next »