Add error if yield is used outside a generator

The yield statement can only be used in generator functions, which are
marked with an asterix.
This commit is contained in:
Nikita Popov 2012-05-19 18:49:27 +02:00
parent 9b51a3b96d
commit fd2a109f86
3 changed files with 27 additions and 1 deletions

View file

@ -0,0 +1,12 @@
--TEST--
Yield cannot be used in normal (non-generator) functions
--FILE--
<?php
function foo() {
yield "Test";
}
?>
--EXPECTF--
Fatal error: The "yield" statement can only be used inside a generator function in %s on line %d

View file

@ -0,0 +1,10 @@
--TEST--
Yield cannot be used outside of functions
--FILE--
<?php
yield "Test";
?>
--EXPECTF--
Fatal error: The "yield" statement can only be used inside a generator function in %s on line %d

View file

@ -2660,7 +2660,11 @@ void zend_do_return(znode *expr, int do_end_vparse TSRMLS_DC) /* {{{ */
void zend_do_yield(znode *expr TSRMLS_DC) /* {{{ */ void zend_do_yield(znode *expr TSRMLS_DC) /* {{{ */
{ {
/* do nothing for now */ if ((CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) == 0) {
zend_error(E_COMPILE_ERROR, "The \"yield\" statement can only be used inside a generator function");
}
/* do nothing for now */
} }
/* }}} */ /* }}} */