Possibly unnecessary binding of variables across arrow functions?
Description
The following code:
<?php
foo(fn($a) => array_map(fn($b) => $a + $b, $array));
Resulted in this (level 2) opcode output:
$_main:
; (lines=7, args=0, vars=2, tmps=1)
; (after optimizer)
; C:\tmp\test\test.php:1-6
0000 INIT_FCALL 1 96 string("foo")
0001 T2 = DECLARE_LAMBDA_FUNCTION 0
0002 BIND_LEXICAL T2 CV0($b)
0003 BIND_LEXICAL T2 CV1($array)
0004 SEND_VAL T2 1
0005 DO_UCALL
0006 RETURN int(1)
LIVE RANGES:
2: 0002 - 0004 (tmp/var)
{closure:C:\tmp\test\test.php:5}:
; (lines=10, args=1, vars=3, tmps=1)
; (after optimizer)
; C:\tmp\test\test.php:5-5
0000 CV0($a) = RECV 1
0001 BIND_STATIC CV1($b)
0002 BIND_STATIC CV2($array)
0003 INIT_FCALL 2 112 string("array_map")
0004 T3 = DECLARE_LAMBDA_FUNCTION 0
0005 BIND_LEXICAL T3 CV0($a)
0006 SEND_VAL T3 1
0007 SEND_VAR CV2($array) 2
0008 V3 = DO_ICALL
0009 RETURN V3
LIVE RANGES:
3: 0005 - 0006 (tmp/var)
{closure:{closure:C:\tmp\test\test.php:5}:5}:
; (lines=4, args=1, vars=2, tmps=1)
; (after optimizer)
; C:\tmp\test\test.php:5-5
0000 CV0($b) = RECV 1
0001 BIND_STATIC CV1($a)
0002 T2 = ADD CV1($a) CV0($b)
0003 RETURN T2
Notice line 0002 of $_main and line 0001 of the outer closure: $b is being bound even though its only appearance and use is local to the inner closure. (While $b does explicitly appear inside the outer arrow function and this would be why it's being captured, the fact that it does so as a parameter of the inner arrow function is being ignored).
I'm not sure if this could cause problems, but it seems unnecessary.
PHP Version
PHP 8.4.12 (cli) (built: Aug 26 2025 21:18:21) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.4.12, Copyright (c) Zend Technologies
with Zend OPcache v8.4.12, Copyright (c), by Zend Technologies
Operating System
Windows 10
Hi @MorganLOCode. This is necessary. Without first binding the value to the outer closure, it can't then bind it to the inner closure. It would work the same way with traditional closures with explicit captures.
This is necessary. Without first binding the value to the outer closure, it can't then bind it to the inner closure.
I don't follow. $b is a parameter in the inner closure and is thus provided by the array_map() call (which takes it from $array). Binding $b in the outer closure is useless, since the value will not be observable.
Oops. I looked at the example wrong. I'm on mobile but will check again later.
find_implicit_binds removes parameters after the run but find_implicit_binds_recursively visits the bodies of nested closures. So only the parameters of the outer closure are removed.
While it's tempting to just add the code to remove parameters to find_implicit_binds_recursively, this isn't safe if $b is also used in the outer closure.
First thing that comes to mind is doing something like this, where we track the "nesting depth" of the first occurrence of a variable, but I'm not sure yet if it's a correct optimization (and not sure if there's a better way):
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 358b6723e00..6c241dda61f 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -8082,7 +8082,22 @@ typedef struct {
bool varvars_used;
} closure_info;
-static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
+static void remove_params_from_binds(closure_info *info, zend_ast *params_ast, zend_long depth) {
+ zend_ast_list *param_list = zend_ast_get_list(params_ast);
+
+ /* Remove variables that are parameters */
+ for (uint32_t i = 0; i < param_list->children; i++) {
+ zend_ast *param_ast = param_list->child[i];
+ zval *zv = zend_hash_find(&info->uses, zend_ast_get_str(param_ast->child[1]));
+ if (zv && Z_LVAL_P(zv) == depth) {
+ ZEND_ASSERT(XtOffsetOf(Bucket, val) == 0);
+ Bucket *bucket = (Bucket *) zv;
+ zend_hash_del_bucket(&info->uses, bucket);
+ }
+ }
+}
+
+static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast, zend_long depth) {
if (!ast) {
return;
}
@@ -8101,16 +8116,18 @@ static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
return;
}
- zend_hash_add_empty_element(&info->uses, name);
+ zval zv;
+ ZVAL_LONG(&zv, depth);
+ zend_hash_add(&info->uses, name, &zv);
} else {
info->varvars_used = true;
- find_implicit_binds_recursively(info, name_ast);
+ find_implicit_binds_recursively(info, name_ast, depth);
}
} else if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
uint32_t i;
for (i = 0; i < list->children; i++) {
- find_implicit_binds_recursively(info, list->child[i]);
+ find_implicit_binds_recursively(info, list->child[i], depth);
}
} else if (ast->kind == ZEND_AST_CLOSURE) {
/* For normal closures add the use() list. */
@@ -8119,18 +8136,21 @@ static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
if (uses_ast) {
zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
uint32_t i;
+ zval zv;
+ ZVAL_LONG(&zv, depth);
for (i = 0; i < uses_list->children; i++) {
- zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
+ zend_hash_add(&info->uses, zend_ast_get_str(uses_list->child[i]), &zv);
}
}
} else if (ast->kind == ZEND_AST_ARROW_FUNC) {
/* For arrow functions recursively check the expression. */
zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
- find_implicit_binds_recursively(info, closure_ast->child[2]);
+ find_implicit_binds_recursively(info, closure_ast->child[2], depth + 1);
+ remove_params_from_binds(info, closure_ast->child[0], depth + 1);
} else if (!zend_ast_is_special(ast)) {
uint32_t i, children = zend_ast_get_num_children(ast);
for (i = 0; i < children; i++) {
- find_implicit_binds_recursively(info, ast->child[i]);
+ find_implicit_binds_recursively(info, ast->child[i], depth);
}
}
}
@@ -8138,17 +8158,12 @@ static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
{
zend_ast_list *param_list = zend_ast_get_list(params_ast);
- uint32_t i;
zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
- find_implicit_binds_recursively(info, stmt_ast);
+ find_implicit_binds_recursively(info, stmt_ast, 0);
- /* Remove variables that are parameters */
- for (i = 0; i < param_list->children; i++) {
- zend_ast *param_ast = param_list->child[i];
- zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
- }
+ remove_params_from_binds(info, params_ast, 0);
}
static void compile_implicit_lexical_binds(
Possibly simpler solution: Could we not just call find_implicit_binds() from find_implicit_binds_recursively() with a fresh closure_info for the nested closure, which already removes its parameters, and then copy them over to the outer closure_info? A bit less efficient, but deeply nested closures are quite unlikely.
PoC:
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 57c012ac71a..a54cce0f557 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -8083,6 +8083,8 @@ typedef struct {
bool varvars_used;
} closure_info;
+static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
+
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
if (!ast) {
return;
@@ -8127,7 +8129,15 @@ static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
} else if (ast->kind == ZEND_AST_ARROW_FUNC) {
/* For arrow functions recursively check the expression. */
zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
- find_implicit_binds_recursively(info, closure_ast->child[2]);
+ closure_info inner_info;
+ find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
+ if (inner_info.varvars_used) {
+ info->varvars_used = true;
+ }
+ if (zend_hash_num_elements(&inner_info.uses)) {
+ zend_hash_copy(&info->uses, &inner_info.uses, NULL);
+ }
+ zend_hash_destroy(&inner_info.uses);
} else if (!zend_ast_is_special(ast)) {
uint32_t i, children = zend_ast_get_num_children(ast);
for (i = 0; i < children; i++) {
diff --git a/ext/opcache/tests/gh19867.phpt b/ext/opcache/tests/gh19867.phpt
new file mode 100644
index 00000000000..5fc345dbb30
--- /dev/null
+++ b/ext/opcache/tests/gh19867.phpt
@@ -0,0 +1,35 @@
+--TEST--
+GH-19867: Avoid binding nested arrow function parameters
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable_cli=1
+opcache.opt_debug_level=0x20000
+--FILE--
+<?php
+fn() => fn($a, $b) => $a + $b;
+?>
+--EXPECTF--
+$_main:
+ ; (lines=%d, args=0, vars=%d, tmps=%d)
+ ; (after optimizer)
+ ; %s
+0000 T0 = DECLARE_LAMBDA_FUNCTION 0
+0001 FREE T0
+0002 RETURN int(1)
+
+{closure:%s:%d}:
+ ; (lines=%d, args=0, vars=%d, tmps=%d)
+ ; (after optimizer)
+ ; %s
+0000 T0 = DECLARE_LAMBDA_FUNCTION 0
+0001 RETURN T0
+
+{closure:{closure:%s:%d}:
+ ; (lines=%d, args=2, vars=%d, tmps=%d)
+ ; (after optimizer)
+ ; %s
+0000 CV0($a) = RECV 1
+0001 CV1($b) = RECV 2
+0002 T2 = ADD CV0($a) CV1($b)
+0003 RETURN T2
I had thought of something like that but wanted to avoid the extra allocation. However you are right that nesting should be rare. So we should likely go with your simpler solution.