Skip to content

Commit 6fb3d92

Browse files
committed
Fixed bug #80334
If assert() was called with named args, add description as named arg as well.
1 parent 4bbe55b commit 6fb3d92

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.0.0RC4
44

5+
- Core:
6+
. Fixed bug #80334 (assert() vs named parameters - confusing error). (Nikita)
7+
58
- FFI:
69
. Fixed bug #79177 (FFI doesn't handle well PHP exceptions within callback).
710
(cmb, Dmitry, Nikita)

Zend/tests/named_params/assert.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Calling assert with named params
3+
--FILE--
4+
<?php
5+
6+
assert(assertion: true);
7+
try {
8+
assert(assertion: false);
9+
} catch (AssertionError $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
13+
assert(assertion: true, description: "Description");
14+
try {
15+
assert(assertion: false, description: "Description");
16+
} catch (AssertionError $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
20+
try {
21+
assert(description: "Description");
22+
} catch (Error $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
26+
?>
27+
--EXPECT--
28+
assert(assertion: false)
29+
Description
30+
Named parameter $description overwrites previous argument

Zend/zend_compile.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3944,13 +3944,18 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
39443944
}
39453945
opline->result.num = zend_alloc_cache_slot();
39463946

3947-
if (args->children == 1 &&
3948-
(args->child[0]->kind != ZEND_AST_ZVAL ||
3949-
Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
3947+
if (args->children == 1) {
39503948
/* add "assert(condition) as assertion message */
3951-
zend_ast_list_add((zend_ast*)args,
3952-
zend_ast_create_zval_from_str(
3953-
zend_ast_export("assert(", args->child[0], ")")));
3949+
zend_ast *arg = zend_ast_create_zval_from_str(
3950+
zend_ast_export("assert(", args->child[0], ")"));
3951+
if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
3952+
/* If the original argument was named, add the new argument as named as well,
3953+
* as mixing named and positional is not allowed. */
3954+
zend_ast *name = zend_ast_create_zval_from_str(
3955+
zend_string_init("description", sizeof("description") - 1, 0));
3956+
arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
3957+
}
3958+
zend_ast_list_add((zend_ast *) args, arg);
39543959
}
39553960

39563961
zend_compile_call_common(result, (zend_ast*)args, fbc);

0 commit comments

Comments
 (0)