Skip to content

Commit 8ed66b4

Browse files
committed
Add tests for list() in assignment in array literals
Array literals will constant evaluate their elements. These can include assignments, even though these are not valid constant expressions. The lhs of assignments can be a list() element (or []) which is parsed as an array with a special flag.
1 parent 58a191c commit 8ed66b4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Zend/tests/gh11320_1.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-11320: Array literals can contain list() assignments
3+
--FILE--
4+
<?php
5+
$index = 1;
6+
function getList() { return [2, 3]; }
7+
var_dump([$index => list($x, $y) = getList()]);
8+
var_dump([$index => [$x, $y] = getList()]);
9+
?>
10+
--EXPECT--
11+
array(1) {
12+
[1]=>
13+
array(2) {
14+
[0]=>
15+
int(2)
16+
[1]=>
17+
int(3)
18+
}
19+
}
20+
array(1) {
21+
[1]=>
22+
array(2) {
23+
[0]=>
24+
int(2)
25+
[1]=>
26+
int(3)
27+
}
28+
}

Zend/tests/gh11320_2.phpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-11320: list() expressions can contain magic constants
3+
--FILE--
4+
<?php
5+
[list(__FILE__ => $foo) = [__FILE__ => 'foo']];
6+
var_dump($foo);
7+
[[__FILE__ => $foo] = [__FILE__ => 'foo']];
8+
var_dump($foo);
9+
?>
10+
--EXPECT--
11+
string(3) "foo"
12+
string(3) "foo"

Zend/tests/gh11320_3.phpt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
GH-11320: list() must not appear as a standalone array element
3+
--FILE--
4+
<?php
5+
[list($a)];
6+
?>
7+
--EXPECTF--
8+
Fatal error: Cannot use list() as standalone expression in %s on line %d

0 commit comments

Comments
 (0)