]> granicus.if.org Git - php/commitdiff
Improve error message for leading comma in keyed list assignment
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 25 Jan 2021 10:12:21 +0000 (11:12 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 25 Jan 2021 10:13:37 +0000 (11:13 +0100)
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.

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

diff --git a/Zend/tests/list_keyed_leading_comma.phpt b/Zend/tests/list_keyed_leading_comma.phpt
new file mode 100644 (file)
index 0000000..dcadcbf
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Leading comma in keyed list assignment
+--FILE--
+<?php
+
+[, "a" => $b] = [1, "a" => 2];
+
+?>
+--EXPECTF--
+Fatal error: Cannot use empty array entries in keyed array assignment in %s on line %d
index d1ea49342e4e7e89037460700ec5e2f8aa2e4b5d..90ab92251a3bdac031423977d8a6bb52fdfd9b86 100644 (file)
@@ -2963,14 +2963,23 @@ static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
 }
 /* }}} */
 
+static bool list_is_keyed(zend_ast_list *list)
+{
+       for (uint32_t i = 0; i < list->children; i++) {
+               if (list->child[i]) {
+                       return list->child[i]->child[1] != NULL;
+               }
+       }
+       return false;
+}
+
 static void zend_compile_list_assign(
                znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
 {
        zend_ast_list *list = zend_ast_get_list(ast);
        uint32_t i;
        bool has_elems = 0;
-       bool is_keyed =
-               list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
+       bool is_keyed = list_is_keyed(list);
 
        if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
                zval_make_interned_string(&expr_node->u.constant);