Skip to content

Commit ad71d35

Browse files
authored
Fix redundant call to ast_to_zval (#230)
I noticed in 1.1.0dev that the __declId did not match a polyfill when short arrow functions were nested. ast_to_zval was already being called on the above lines in ast.c, calling it again would create `ast\Node` instances again.
1 parent e460595 commit ad71d35

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ static void ast_fill_children_ht(HashTable *ht, zend_ast *ast, ast_state_info_t
795795
/* (This is still different from regular functions, which have AST_STMT_LIST) */
796796
/* TODO: In a new node type, remove the ZEND_AST_RETURN node instead. */
797797
zval tmp;
798-
ast_to_zval(&tmp, child, state);
798+
ZVAL_COPY_VALUE(&tmp, &child_zv);
799799
ast_create_virtual_node_ex(
800800
&child_zv, ZEND_AST_RETURN, 0, zend_ast_get_lineno(child), state, 1, &tmp);
801801
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<file name="prop_doc_comments.phpt" role="test" />
123123
<file name="short_arrow_function.phpt" role="test" />
124124
<file name="short_arrow_function_return.phpt" role="test" />
125+
<file name="short_arrow_function_decl_id.phpt" role="test" />
125126
<file name="stmt_list.phpt" role="test" />
126127
<file name="try_catch_finally.phpt" role="test" />
127128
<file name="type_hints.phpt" role="test" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Nested arrow functions in PHP 7.4
3+
--SKIPIF--
4+
<?php if (PHP_VERSION_ID < 70400) die('skip PHP >= 7.4 only'); ?>
5+
--FILE--
6+
<?php
7+
8+
require __DIR__ . '/../util.php';
9+
10+
$code = <<<'PHP'
11+
<?php
12+
$cb = fn() => fn() => $undef;
13+
PHP;
14+
15+
$node = ast\parse_code($code, $version=85);
16+
echo ast_dump($node) . "\n";
17+
?>
18+
--EXPECT--
19+
AST_STMT_LIST
20+
0: AST_ASSIGN
21+
var: AST_VAR
22+
name: "cb"
23+
expr: AST_ARROW_FUNC
24+
flags: 0
25+
name: "{closure}"
26+
docComment: null
27+
params: AST_PARAM_LIST
28+
stmts: AST_RETURN
29+
expr: AST_ARROW_FUNC
30+
flags: 0
31+
name: "{closure}"
32+
docComment: null
33+
params: AST_PARAM_LIST
34+
stmts: AST_RETURN
35+
expr: AST_VAR
36+
name: "undef"
37+
returnType: null
38+
attributes: null
39+
__declId: 0
40+
returnType: null
41+
attributes: null
42+
__declId: 1

0 commit comments

Comments
 (0)