Skip to content

Fix arrow functions not capturing variables in partial application args - #25732

Merged
arnaud-lb merged 4 commits into
php:PHP-8.6from
mihaliak:fix-pfa-arrow-fn-implicit-bind
Nov 25, 2026
Merged

arnaud-lb merged 4 commits into
php:PHP-8.6from
mihaliak:fix-pfa-arrow-fn-implicit-bind

Conversation

@mihaliak

Copy link
Copy Markdown
Contributor

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:

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.

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
arnaud-lb merged commit 7a49329 into php:PHP-8.6 Nov 25, 2026
1 of 2 checks passed
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)
@arnaud-lb

Copy link
Copy Markdown
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
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants