diff options
author | Nikita Popov <nikic@php.net> | 2012-07-21 21:05:46 +0200 |
---|---|---|
committer | Nikita Popov <nikic@php.net> | 2012-07-22 01:22:22 +0200 |
commit | 80748631aa1c4193cbc68f8854d82e7a57817fe2 (patch) | |
tree | ee081901714fba5399cd0aab9ac7f7ab9c8e79af /Zend/tests/generators | |
parent | 1f70a4c5fea97aa577aa5d9ee5f33d91d70e690d (diff) | |
download | php-git-80748631aa1c4193cbc68f8854d82e7a57817fe2.tar.gz |
Require parenthesis around yield expressions
If yield is used in an expression context parenthesis are now required.
This ensures that the code is unambiguos.
Yield statements can still be used without parenthesis (which should be
the most common case).
Also yield expressions without value can be used without parenthesis,
too (this should be the most common case for coroutines).
If the yield expression is used in a context where parenthesis are required
anyway, no additional parenthesis have to be inserted.
Examples:
// Statements don't need parenthesis
yield $foo;
yield $foo => $bar;
// Yield without value doesn't need parenthesis either
$data = yield;
// Parentheses don't have to be duplicated
foo(yield $bar);
if (yield $bar) { ... }
// But we have to use parentheses here
$foo = (yield $bar);
This commit also fixes an issue with by-ref passing of $foo[0] like
variables. They previously weren't properly fetched for write.
Additionally this fixes valgrind warnings which were caused by access to
uninitialized memory in zend_is_function_or_method_call().
Diffstat (limited to 'Zend/tests/generators')
-rw-r--r-- | Zend/tests/generators/send_returns_current.phpt | 2 | ||||
-rw-r--r-- | Zend/tests/generators/yield_array_offset_by_ref.phpt | 26 | ||||
-rw-r--r-- | Zend/tests/generators/yield_in_parenthesis.phpt | 23 |
3 files changed, 50 insertions, 1 deletions
diff --git a/Zend/tests/generators/send_returns_current.phpt b/Zend/tests/generators/send_returns_current.phpt index fc260c0af0..27ba74bc1b 100644 --- a/Zend/tests/generators/send_returns_current.phpt +++ b/Zend/tests/generators/send_returns_current.phpt @@ -6,7 +6,7 @@ $generator->send() returns the yielded value function reverseEchoGenerator() { $data = yield; while (true) { - $data = yield strrev($data); + $data = (yield strrev($data)); } } diff --git a/Zend/tests/generators/yield_array_offset_by_ref.phpt b/Zend/tests/generators/yield_array_offset_by_ref.phpt new file mode 100644 index 0000000000..544108e64d --- /dev/null +++ b/Zend/tests/generators/yield_array_offset_by_ref.phpt @@ -0,0 +1,26 @@ +--TEST-- +Array offsets can be yielded by reference +--FILE-- +<?php + +function &gen(array &$array) { + yield $array[0]; +} + +$array = [1, 2, 3]; +$gen = gen($array); +foreach ($gen as &$val) { + $val *= -1; +} +var_dump($array); + +?> +--EXPECT-- +array(3) { + [0]=> + &int(-1) + [1]=> + int(2) + [2]=> + int(3) +} diff --git a/Zend/tests/generators/yield_in_parenthesis.phpt b/Zend/tests/generators/yield_in_parenthesis.phpt new file mode 100644 index 0000000000..4a603f4cc1 --- /dev/null +++ b/Zend/tests/generators/yield_in_parenthesis.phpt @@ -0,0 +1,23 @@ +--TEST-- +No additional parenthesis are required around yield if they are already present +--FILE-- +<?php + +function gen() { + if (yield $foo); elseif (yield $foo); + if (yield $foo): elseif (yield $foo): endif; + while (yield $foo); + do {} while (yield $foo); + switch (yield $foo) {} + (yield $foo); + die(yield $foo); + func(yield $foo); + $foo->func(yield $foo); + new Foo(yield $foo); +} + +echo "Done"; + +?> +--EXPECT-- +Done |