Skip to content

Commit 321bd6b

Browse files
committed
Improve error message for leading comma in keyed list assignment
Print "Cannot use empty array entries in keyed array assignment" instead of "Cannot mix keyed and unkeyed array entries in assignments" for a leading comma.
1 parent 4846913 commit 321bd6b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Leading comma in keyed list assignment
3+
--FILE--
4+
<?php
5+
6+
[, "a" => $b] = [1, "a" => 2];
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use empty array entries in keyed array assignment in %s on line %d

Zend/zend_compile.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,14 +2963,23 @@ static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
29632963
}
29642964
/* }}} */
29652965

2966+
static bool list_is_keyed(zend_ast_list *list)
2967+
{
2968+
for (uint32_t i = 0; i < list->children; i++) {
2969+
if (list->child[i]) {
2970+
return list->child[i]->child[1] != NULL;
2971+
}
2972+
}
2973+
return false;
2974+
}
2975+
29662976
static void zend_compile_list_assign(
29672977
znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
29682978
{
29692979
zend_ast_list *list = zend_ast_get_list(ast);
29702980
uint32_t i;
29712981
bool has_elems = 0;
2972-
bool is_keyed =
2973-
list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
2982+
bool is_keyed = list_is_keyed(list);
29742983

29752984
if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
29762985
zval_make_interned_string(&expr_node->u.constant);

0 commit comments

Comments
 (0)