summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikic@php.net>2012-05-19 18:49:27 +0200
committerNikita Popov <nikic@php.net>2012-05-19 18:49:27 +0200
commitfd2a109f86d18b93d29153c2ddfa605651c7df05 (patch)
treefc605300bc557a5ff64a7f4ea253b5343450989e
parent9b51a3b96d89dc10d4ea65ceacaad22fa3420ea7 (diff)
downloadphp-git-fd2a109f86d18b93d29153c2ddfa605651c7df05.tar.gz
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.
-rw-r--r--Zend/tests/generators/yield_in_normal_function_error.phpt12
-rw-r--r--Zend/tests/generators/yield_outside_function_error.phpt10
-rw-r--r--Zend/zend_compile.c6
3 files changed, 27 insertions, 1 deletions
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 0000000000..802510d29c
--- /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 0000000000..fd7169d5f2
--- /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 0d3ea109bf..d2b3536788 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 */
}
/* }}} */