[php-src] PR #25732: Fix arrow functions not capturing variables in partial application args
| From: | mihaliak | Date: | Wed, 25 Nov 2026 08:12:03 +0000 |
| Subject: | [php-src] PR #25732: Fix arrow functions not capturing variables in partial application args | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40989@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25732
Author: mihaliak
An arrow function does not capture a variable that it uses only in the bound arguments of a partial
application. The
variable is undefined inside the arrow function, so the partial application binds
null
and PHP emits an "Undefined
variable" warning:
```php
function f($a, $b) {
return "$a $b";
}
$a = 'a';
var_dump((fn() => f($a, ?))()('b'));
// Warning: Undefined variable $a
// string(2) " b"
var_dump((fn() => array_map(f($a, ?), ['b'])[0])());
// Warning: Undefined variable $a
// string(2) " b"
```
The equivalent closure, fn() => array_map(fn($b) => f($a, $b),
['b'])[0], and a long closure with use ($a) both
work. The variable is also captured if it appears anywhere else in the arrow function's body.
### Cause
find_implicit_binds_recursively() decides which variables an arrow function captures by
walking the child[] nodes
of its body. A partial application's arguments are kept in zend_ast_fcc.args on
its ZEND_AST_CALLABLE_CONVERT
node, which declares no children, so the walk never reaches them. Before partial application, this
node only stood for
f(...), which has no arguments to capture.
### Fix
Walk zend_ast_fcc.args when the walker meets a ZEND_AST_CALLABLE_CONVERT
node, as zend_ast_apply() and the
opcache persistence code already do.
### Tests
Zend/tests/partial_application/arrow_function_implicit_bind.phpt covers a partial
application of a function, a method
and a static method, named and variadic placeholders, a partial application nested in another call
and in the bound
arguments, an arrow function in the bound arguments, and capture by value at creation. It fails
without the fix.
Checked on macOS (arm64), minimal debug builds (--enable-debug --disable-all):
- NTS: make test passes, 0 failures.
- NTS with opcache.enable_cli=1, also with opcache.jit=tracing:
Zend/tests/partial_application,
Zend/tests/arrow_functions and Zend/tests/closures pass.
- ZTS (--enable-zts): builds without warnings, and the same three directories pass.