]> granicus.if.org Git - php/commitdiff
Fixed bug #80334
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 9 Nov 2020 09:18:43 +0000 (10:18 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 9 Nov 2020 09:19:32 +0000 (10:19 +0100)
If assert() was called with named args, add description as named
arg as well.

NEWS
Zend/tests/named_params/assert.phpt [new file with mode: 0644]
Zend/zend_compile.c

diff --git a/NEWS b/NEWS
index 9e7efeaf974437e5bed6797dfbd08be3ee754529..2039265620c462cae5ff0e67f9009ae6eb22b89d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? ????, PHP 8.0.0RC4
 
+- Core:
+  . Fixed bug #80334 (assert() vs named parameters - confusing error). (Nikita)
+
 - FFI:
   . Fixed bug #79177 (FFI doesn't handle well PHP exceptions within callback).
     (cmb, Dmitry, Nikita)
diff --git a/Zend/tests/named_params/assert.phpt b/Zend/tests/named_params/assert.phpt
new file mode 100644 (file)
index 0000000..40a92a2
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Calling assert with named params
+--FILE--
+<?php
+
+assert(assertion: true);
+try {
+    assert(assertion: false);
+} catch (AssertionError $e) {
+    echo $e->getMessage(), "\n";
+}
+
+assert(assertion: true, description: "Description");
+try {
+    assert(assertion: false, description: "Description");
+} catch (AssertionError $e) {
+    echo $e->getMessage(), "\n";
+}
+
+try {
+    assert(description: "Description");
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+
+?>
+--EXPECT--
+assert(assertion: false)
+Description
+Named parameter $description overwrites previous argument
index 396cfd91c0c213403480be7418596cb6c58f12ad..dbb21944371f49dc48d56e1bcd4aef0febd17b07 100644 (file)
@@ -3944,13 +3944,18 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
                }
                opline->result.num = zend_alloc_cache_slot();
 
-               if (args->children == 1 &&
-                   (args->child[0]->kind != ZEND_AST_ZVAL ||
-                    Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
+               if (args->children == 1) {
                        /* add "assert(condition) as assertion message */
-                       zend_ast_list_add((zend_ast*)args,
-                               zend_ast_create_zval_from_str(
-                                       zend_ast_export("assert(", args->child[0], ")")));
+                       zend_ast *arg = zend_ast_create_zval_from_str(
+                               zend_ast_export("assert(", args->child[0], ")"));
+                       if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
+                               /* If the original argument was named, add the new argument as named as well,
+                                * as mixing named and positional is not allowed. */
+                               zend_ast *name = zend_ast_create_zval_from_str(
+                                       zend_string_init("description", sizeof("description") - 1, 0));
+                               arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
+                       }
+                       zend_ast_list_add((zend_ast *) args, arg);
                }
 
                zend_compile_call_common(result, (zend_ast*)args, fbc);