php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78434 Generator yields no items after valid() call
Submitted: 2019-08-21 10:02 UTC Modified: 2020-05-20 19:19 UTC
From: enumag at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 7.3.11 OS: irrelevant
Private report: No CVE-ID: None
 [2019-08-21 10:02 UTC] enumag at gmail dot com
Description:
------------
See the example script. The inner $function generator is called twice so two zeros should be yielded from $wrapper. Yet only one is yielded if I call valid() on the Generator before using yield from.

Test script:
---------------
https://3v4l.org/GfJHG

Expected result:
----------------
00

Actual result:
--------------
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-21 13:03 UTC] nikic@php.net
The correct behavior here depends on whether "yield from" is supposed to iterate from the current position of the generator, or from the start (and throw if we're not at the start). To make this more obvious, replace valid() with current() -- should the yield from repeat the value you already fetched?

Generally I would agree that it should iterate from the start, in particular because this is what yield from does for non-generator Iterators.
 [2019-08-21 13:24 UTC] enumag at gmail dot com
> To make this more obvious, replace valid() with current() -- should the yield from repeat the value you already fetched?

Of course it should. I didn't call next() so the first value was not yet "consumed".

Also as you can see in the example the behaviour is inconsistent between the first and second yield from. That's definitely not correct.
 [2019-08-21 13:28 UTC] enumag at gmail dot com
As far as I know calling Generator::rewind() is alright until next() is called for the first time. Which means that your argument whether or not should yield from call rewind() is not relevant here - the result would be the same regardless.
 [2019-08-23 15:53 UTC] nikic@php.net
-Status: Open +Status: Verified
 [2019-08-23 15:53 UTC] nikic@php.net
Sorry, I missed the different behavior for the two yield froms... something is definitely broken here.
 [2019-11-07 11:26 UTC] nikic@php.net
-PHP Version: 7.4.0beta2 +PHP Version: 7.3.11
 [2020-04-02 09:48 UTC] enumag at gmail dot com
Bump. Just ran into this again, it is really bothering me that I can't rely on Generators to work correctly. Can you please fix this?
 [2020-04-02 15:03 UTC] nikic@php.net
It looks like the difference in behavior is because the first yield from runs with DO_INIT flag set, which prevents the initial advance.
 [2020-04-03 10:10 UTC] nikic@php.net
Candidate patch at https://github.com/php/php-src/pull/5344. Very much not sure this is right.
 [2020-04-09 08:36 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=823a956855c1f38ed8ee888bf248917e28d40314
Log: Fixed bug #78434
 [2020-04-09 08:36 UTC] nikic@php.net
-Status: Verified +Status: Closed
 [2020-05-20 19:04 UTC] grzegorz at kielak dot me
have we just created a new bug?

https://3v4l.org/S4PAK
 [2020-05-20 19:19 UTC] requinix@php.net
-Assigned To: +Assigned To: requinix
 [2020-05-20 19:19 UTC] requinix@php.net
-Assigned To: requinix +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC