Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions Zend/tests/array_unpack/already_occupied.phpt

This file was deleted.

11 changes: 11 additions & 0 deletions Zend/tests/array_unpack/with_keys_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Cannot unpack in array with keys
--FILE--
<?php

$ary = [1, 2, 3];
$ary2 = [42 => 0, ...$ary];

?>
--EXPECTF--
Fatal error: Cannot use unpacking in arrays with explicit keys in %s on line %d
11 changes: 11 additions & 0 deletions Zend/tests/array_unpack/with_keys_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Cannot unpack in array with keys (2)
--FILE--
<?php

$ary = [1, 2, 3];
$ary2 = [...$ary, 'a' => 4];

?>
--EXPECTF--
Fatal error: Cannot use unpacking in arrays with explicit keys in %s on line %d
11 changes: 11 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6946,6 +6946,8 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
zend_ast *last_elem_ast = NULL;
uint32_t i;
zend_bool is_constant = 1;
zend_bool has_explicit_keys = 0;
uint32_t unpack_lineno = (uint32_t)-1;

if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
Expand All @@ -6972,7 +6974,11 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
) {
is_constant = 0;
}
if (elem_ast->child[1]) {
has_explicit_keys = 1;
}
} else {
unpack_lineno = zend_ast_get_lineno(elem_ast);
zend_eval_const_expr(&elem_ast->child[0]);

if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
Expand All @@ -6983,6 +6989,11 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
last_elem_ast = elem_ast;
}

if (has_explicit_keys && unpack_lineno != (uint32_t) -1) {
CG(zend_lineno) = unpack_lineno;
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use unpacking in arrays with explicit keys");
}

if (!is_constant) {
return 0;
}
Expand Down