Skip to content

Commit

Permalink
Fixed assign coalesce. "$a[0] ??= $a" should evaluate the right $a fi…
Browse files Browse the repository at this point in the history
…rst.
  • Loading branch information
dstogov committed Oct 5, 2021
1 parent 0eb7a15 commit 69fb20f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Zend/tests/assign_coalesce_007.phpt
@@ -0,0 +1,13 @@
--TEST--
Assign coalesce: "$a[0] ??= $a" should evaluate the right $a first
--FILE--
<?php
$a[0] ??= $a;
var_dump($a);
?>
--EXPECTF--
Warning: Undefined variable $a in %sassign_coalesce_007.php on line 2
array(1) {
[0]=>
NULL
}
15 changes: 14 additions & 1 deletion Zend/zend_compile.c
Expand Up @@ -8510,7 +8510,20 @@ void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);

CG(memoize_mode) = ZEND_MEMOIZE_NONE;
zend_compile_expr(&default_node, default_ast);
if (var_ast->kind == ZEND_AST_DIM
&& zend_is_assign_to_self(var_ast, default_ast)
&& !is_this_fetch(default_ast)) {
/* $a[0] = $a should evaluate the right $a first */
znode cv_node;

if (zend_try_compile_cv(&cv_node, default_ast) == FAILURE) {
zend_compile_simple_var_no_cv(&default_node, default_ast, BP_VAR_R, 0);
} else {
zend_emit_op_tmp(&default_node, ZEND_QM_ASSIGN, &cv_node, NULL);
}
} else {
zend_compile_expr(&default_node, default_ast);
}

CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
zend_compile_var(&var_node_w, var_ast, BP_VAR_W, 0);
Expand Down

0 comments on commit 69fb20f

Please sign in to comment.