Skip to content

Infer the return type of a closure calling itself through a by-ref use - #7216

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
recursive-closure-return
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
recursive-closure-return

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Follow-up to #7212 and #7213.

A closure that calls itself through a by-ref use had its return type computed with the recursive call typed from a walk that entered the by-ref variable as mixed, so the self-call contributed mixed or a benevolent arithmetic union:

$fact = function (int $n) use (&$fact) {
	return $n <= 1 ? 1 : $n * $fact($n - 1);
};
$fact(5); // (float|int), now int<1, max>

$countdown = function (int $n) use (&$countdown) {
	return $n === 0 ? 'done' : $countdown($n - 1);
};
$countdown(3); // mixed, now 'done'

When ClosureProcessor types the by-ref use of the variable the closure is assigned to, and the right side is the closure itself without a declared return type, resolveAssignedClosureType() now finds the least fixed point of its return:

  1. The variable starts as the closure returning never.
  2. Each round walks the body with the previous round's closure in the variable, and joins the return.
  3. The rounds stop when the return stops growing, generalized after the first round like a loop (LOOP_SCOPE_ITERATIONS / GENERALIZE_AFTER_ITERATION, generalizeIteratedType() from Resolve accumulator templates of a closure to a fixed point #7212). Without a fixed point within them, the type resolved as before stays.

Starting from never rather than from today's type matters: a round that already sees float|int from the self-call can only reproduce it.

To walk the body with the seeded variable, ClosureTypeResolver::getClosureType() takes a new optional $byRefUseTypes parameter. It types those by-ref uses in the walk, which otherwise enters them as mixed, and it is part of the cache key. ClosureTypeResolver is not @api. Both changes are mirrored in turbo-ext/src/ClosureProcessor.cpp and ClosureTypeResolver.cpp.

Changed expectation, more precise and sound:

  • nsrt/closure-signature-from-usages.php recursion(): the untyped $fact($n), whose parameter is inferred as float|int from its usages, returns NAN|float<1.0000000000000002, max>|int<1, max> instead of float|int. The base case returns 1, and the recursive branch runs only for $n > 1 or NAN.

Tests:

  • New nsrt/recursive-closure-return-type.php. Without the source change, four of its assertions fail. A closure with a declared return type ($fib) is the control and stays int.
  • The full suite passes with and without the extension, with identical output. Self-analysis is clean in both modes. side-by-side.php, signature-parity.php and smoke.php pass.

A closure assigned to the variable it uses by reference, as in
$f = function () use (&$f), is now typed by a fixed point of its return:
the variable starts as the closure returning never, each round walks the body
with the previous round's closure, and the rounds widen as a loop does.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c3c-cedc-7330-a083-cc986590888f
@ondrejmirtes
ondrejmirtes merged commit c74849e into 2.3.x Nov 27, 2026
889 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the recursive-closure-return branch November 27, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant