Description
Closures cannot capture variables by reference using the use (&$var) syntax.
This is required for many recursive closures and algorithms that need to mutate outer scope variables.
Reproduction
<?php
$g = null;
$g = function ($n) use (&$g) {
return $n <= 1 ? 1 : $n * $g($n - 1);
};
echo $g(5); // Expected: 120 (5!)
Expected behavior
The closure should capture $g by reference so the recursive call works.
Actual behavior
Parser error:
error[...]: Expected variable in use() capture list
error[...]: Expected '=' after variable name
Environment
elephc 0.21.8
Possible root cause
The closure parameter / capture parsing logic (likely in src/parser/stmt/params.rs or the closure parsing code in src/parser/expr/) does not recognize the & before a captured variable name in the use() list.
The Closure variant in ExprKind has a captures: Vec<String> field, but the parser never populates reference information.
Additional context
This pattern is the standard way to implement recursive anonymous functions in PHP before static variables or fn recursion.
Stress case: functions/closure_recursive.php
Labels: bug, parser, php-compatibility, closures
Description
Closures cannot capture variables by reference using the
use (&$var)syntax.This is required for many recursive closures and algorithms that need to mutate outer scope variables.
Reproduction
Expected behavior
The closure should capture
$gby reference so the recursive call works.Actual behavior
Parser error:
Environment
elephc 0.21.8
Possible root cause
The closure parameter / capture parsing logic (likely in
src/parser/stmt/params.rsor the closure parsing code insrc/parser/expr/) does not recognize the&before a captured variable name in theuse()list.The
Closurevariant inExprKindhas acaptures: Vec<String>field, but the parser never populates reference information.Additional context
This pattern is the standard way to implement recursive anonymous functions in PHP before
staticvariables orfnrecursion.Stress case:
functions/closure_recursive.phpLabels:
bug,parser,php-compatibility,closures