Commit:    fd2a109f86d18b93d29153c2ddfa605651c7df05
Author:    Nikita Popov <ni...@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 */
 }
 /* }}} */


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to