Skip to content

Fixed bug #78434 #5344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Zend/tests/generators/bug78434.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Bug #78434: Generator yields no items after valid() call
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit wrong in the bug description. The generator just skips the first item. See https://3v4l.org/2HBSX

Suggested change
Bug #78434: Generator yields no items after valid() call
Bug #78434: Generator skips first item after valid() call

--FILE--
<?php

$function = function () {
yield 0;
};

$wrapper = function () use ($function) {
$generator = $function();
$generator->valid();
yield from $generator;

$generator = $function();
// Comment out this line to get the correct result.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Comment out this line to get the correct result.

$generator->valid();
yield from $generator;
};

foreach ($wrapper() as $value) {
echo $value, "\n";
}

?>
--EXPECT--
0
0
3 changes: 0 additions & 3 deletions Zend/tests/generators/yield_from_multi_tree.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ function from($levels) {
}

function gen($gen, $level) {
if ($level % 2) {
yield $gen->current();
}
yield from $gen;
}

Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ void zend_generator_yield_from(zend_generator *generator, zend_generator *from)
generator->node.parent = from;
zend_generator_get_current(generator);
GC_DELREF(&from->std);
generator->flags |= ZEND_GENERATOR_DO_INIT;
}

ZEND_API zend_generator *zend_generator_update_current(zend_generator *generator, zend_generator *leaf)
Expand Down Expand Up @@ -786,6 +787,7 @@ ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */

if (UNEXPECTED((orig_generator->flags & ZEND_GENERATOR_DO_INIT) != 0 && !Z_ISUNDEF(generator->value))) {
/* We must not advance Generator if we yield from a Generator being currently run */
orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
return;
}

Expand Down Expand Up @@ -865,15 +867,15 @@ ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
goto try_again;
}
}

orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
}
/* }}} */

static inline void zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
{
if (UNEXPECTED(Z_TYPE(generator->value) == IS_UNDEF) && EXPECTED(generator->execute_data) && EXPECTED(generator->node.parent == NULL)) {
generator->flags |= ZEND_GENERATOR_DO_INIT;
zend_generator_resume(generator);
generator->flags &= ~ZEND_GENERATOR_DO_INIT;
generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
}
}
Expand Down