Skip to content

Commit 9548f13

Browse files
committed
Make sure new arguments are pre-evaluated
The way we do this is quite fragile, and I've fallen into the same trap in the past. This could use some refactoring.
1 parent 9b28ad9 commit 9548f13

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Check that const exprs are pre-evaluated in new arguments
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
public function __construct(public $x) {}
8+
}
9+
function test(
10+
$a = new C(__CLASS__),
11+
$b = new C(__FUNCTION__),
12+
$c = new C(x: __FILE__),
13+
) {
14+
var_dump($a, $b, $c);
15+
}
16+
test();
17+
18+
?>
19+
--EXPECTF--
20+
object(C)#1 (1) {
21+
["x"]=>
22+
string(0) ""
23+
}
24+
object(C)#2 (1) {
25+
["x"]=>
26+
string(4) "test"
27+
}
28+
object(C)#3 (1) {
29+
["x"]=>
30+
string(%d) "%snew_arg_eval.php"
31+
}

Zend/zend_compile.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10164,6 +10164,25 @@ void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
1016410164
}
1016510165
break;
1016610166
}
10167+
// TODO: We should probably use zend_ast_apply to recursively walk nodes without
10168+
// special handling. It is required that all nodes that are part of a const expr
10169+
// are visited. Probably we should be distinguishing evaluation of const expr and
10170+
// normal exprs here.
10171+
case ZEND_AST_ARG_LIST:
10172+
{
10173+
zend_ast_list *list = zend_ast_get_list(ast);
10174+
for (uint32_t i = 0; i < list->children; i++) {
10175+
zend_eval_const_expr(&list->child[i]);
10176+
}
10177+
return;
10178+
}
10179+
case ZEND_AST_NEW:
10180+
zend_eval_const_expr(&ast->child[0]);
10181+
zend_eval_const_expr(&ast->child[1]);
10182+
return;
10183+
case ZEND_AST_NAMED_ARG:
10184+
zend_eval_const_expr(&ast->child[1]);
10185+
return;
1016710186
default:
1016810187
return;
1016910188
}

0 commit comments

Comments
 (0)