mirror of
https://github.com/php/php-src.git
synced 2025-08-19 17:04:47 +02:00
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:
parent
9b51a3b96d
commit
fd2a109f86
3 changed files with 27 additions and 1 deletions
12
Zend/tests/generators/yield_in_normal_function_error.phpt
Normal file
12
Zend/tests/generators/yield_in_normal_function_error.phpt
Normal 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
|
10
Zend/tests/generators/yield_outside_function_error.phpt
Normal file
10
Zend/tests/generators/yield_outside_function_error.phpt
Normal 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
|
|
@ -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 */
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue