]> granicus.if.org Git - php/commitdiff
Forbid yield from in by-reference generators
authorNikita Popov <nikic@php.net>
Fri, 12 Feb 2016 16:55:29 +0000 (17:55 +0100)
committerNikita Popov <nikic@php.net>
Fri, 12 Feb 2016 16:55:29 +0000 (17:55 +0100)
The current yield from implementation doesn't support by-ref
yields. It's likely not worthwhile to actually implement this,
but we should at least keep the door open for the future by
issuing a compile error.

Refs bug #71252.

Zend/tests/generators/yield_from_by_reference.phpt [new file with mode: 0644]
Zend/zend_compile.c

diff --git a/Zend/tests/generators/yield_from_by_reference.phpt b/Zend/tests/generators/yield_from_by_reference.phpt
new file mode 100644 (file)
index 0000000..8412a32
--- /dev/null
@@ -0,0 +1,12 @@
+--TEST--
+Yield from by reference is not supported
+--FILE--
+<?php
+
+function &gen() {
+    yield from [];
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot use "yield from" inside a by-reference generator in %s on line %d
index 58be2ed2e884277ea7d70ec202dadb0fd438ebd6..56da2962bd8b747573591cdc163a072b5b32114b 100644 (file)
@@ -6294,6 +6294,11 @@ void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
 
        zend_mark_function_as_generator();
 
+       if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
+               zend_error_noreturn(E_COMPILE_ERROR,
+                       "Cannot use \"yield from\" inside a by-reference generator");
+       }
+
        zend_compile_expr(&expr_node, expr_ast);
        zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
 }