com php-src: Add error if yield is used outside a generator: Zend/tests/generators/yield_in_normal_function_error. phpt
Zend/tests/generators/yield_outside_function_error .phpt Zend/zend_compile.c
| From: | Gustavo André dos Santos Lopes | Date: | Sat, 19 May 2012 16:49:27 +0000 |
| Subject: | com php-src: Add error if yield is used outside a generator: Zend/tests/generators/yield_in_normal_function_error. phpt Zend/tests/generators/yield_outside_function_error .phpt Zend/zend_compile.c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-70086@lists.php.net to get a copy of this message | ||
Commit: fd2a109f86d18b93d29153c2ddfa605651c7df05
Author: Nikita Popov <nikic@php.net> Sat, 19 May 2012 18:49:27 +0200
Parents: 9b51a3b96d89dc10d4ea65ceacaad22fa3420ea7
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=fd2a109f86d18b93d29153c2ddfa605651c7df05
Log:
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.
Changed paths:
A Zend/tests/generators/yield_in_normal_function_error.phpt
A Zend/tests/generators/yield_outside_function_error.phpt
M Zend/zend_compile.c
Diff:
diff --git a/Zend/tests/generators/yield_in_normal_function_error.phpt
b/Zend/tests/generators/yield_in_normal_function_error.phpt
new file mode 100644
index 0000000..802510d
--- /dev/null
+++ b/Zend/tests/generators/yield_in_normal_function_error.phpt
@@ -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
diff --git a/Zend/tests/generators/yield_outside_function_error.phpt
b/Zend/tests/generators/yield_outside_function_error.phpt
new file mode 100644
index 0000000..fd7169d
--- /dev/null
+++ b/Zend/tests/generators/yield_outside_function_error.phpt
@@ -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
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 0d3ea10..d2b3536 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -2660,7 +2660,11 @@ void zend_do_return(znode *expr, int do_end_vparse 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 */
}
/* }}} */