Repository navigation
Fix arrow functions not capturing variables in partial application args - #25732
Merged
Merged
Conversation
find_implicit_binds_recursively() walks the child nodes of an arrow function's body to find the variables it captures. The arguments of a partial application are kept in zend_ast_fcc.args instead, so a variable used only there was not captured: it was undefined in the arrow function and the partial application bound null.
* PHP-8.6: Fix float literals losing precision in AST export (phpGH-25461) ext/session: don't follow symlinked subdirectories in files GC (phpGH-25671)
arnaud-lb
added a commit
that referenced
this pull request
Nov 25, 2026
* PHP-8.6: Fix arrow functions not capturing variables in partial application args (#25732)
Member
|
Thank you! |
arnaud-lb
added a commit
to prateekbhujel/php-src
that referenced
this pull request
Nov 25, 2026
* PHP-8.6: (92 commits) Fix arrow functions not capturing variables in partial application args (php#25732) Fix float literals losing precision in AST export (phpGH-25461) ext/session: don't follow symlinked subdirectories in files GC (phpGH-25671) [skip ci] Skip automatic_worker_limit.phpt on few cores [skip ci] Fix Symfony test without USE_TRACKED_ALLOC [skip ci] Skip assign_dim_undef_op_data_008.phpt when JIT is unavailable ext/intl: Preserve the previous MessageFormatter pattern on failure (php#25642) Fix phpGH-25313: Assertion !EG(exception) in ZEND_FE_FREE when unpacking a generator into a by-ref parameter throws ext/opcache: Fix JIT register allocation for ASSIGN to traced reference Fix phpGH-25650: PDO::query() reports wrong argument numbers for ctor args Fix tracing JIT taking the wrong branch after a VM interrupt (php#25636) [skip ci] Add APP_DEBUG=0 for Symfony Preloading test streams: fix stale readbuflen after stream_filter_remove() on a read filter Update NEWS for phpGH-20920 LibreSSL build fix Fix phpGH-20920: do not use OPENSSL_strndup() for peer CN matching Simplify php_openssl_load_all_certs_from_file() Fix phpGH-20920: only register PKCS7_NO_DUAL_CONTENT when it is defined Fix phpGH-20920: guard ENGINE_cleanup() with OPENSSL_NO_ENGINE again ext/soap: Fix NULL dereference on empty Body with matching SOAPAction (phpGH-25661) ext/dom: Fix use-after-free in getElementById() after removing an ID attribute ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
nulland PHP emits an "Undefinedvariable" warning:
The equivalent closure,
fn() => array_map(fn($b) => f($a, $b), ['b'])[0], and a long closure withuse ($a)bothwork. 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 thechild[]nodesof its body. A partial application's arguments are kept in
zend_ast_fcc.argson itsZEND_AST_CALLABLE_CONVERTnode, 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.argswhen the walker meets aZEND_AST_CALLABLE_CONVERTnode, aszend_ast_apply()and theopcache persistence code already do.
Tests
Zend/tests/partial_application/arrow_function_implicit_bind.phptcovers a partial application of a function, a methodand 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):make testpasses, 0 failures.opcache.enable_cli=1, also withopcache.jit=tracing:Zend/tests/partial_application,Zend/tests/arrow_functionsandZend/tests/closurespass.--enable-zts): builds without warnings, and the same three directories pass.