Skip to content

Solve accumulator templates that cycle through several closures together - #7213

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
template-cycle-solver
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
template-cycle-solver

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Follow-up to #7212.

#7212 solves an accumulator that one closure feeds back to itself, as the TCarry of callable(TCarry, T): TCarry. A template can also cycle through several closures: one closure returns it, and another one takes it. Before this PR, every closure was typed on its own, so the cycle never ran more than once:

/**
 * @template T
 * @template U
 * @param T $init
 * @param callable(T): U $f
 * @param callable(U): T $g
 * @return T
 */
function pingPong($init, callable $f, callable $g) { ... }

pingPong(0, static fn (int $t) => [$t], static fn (array $u) => $u[0] + 1);
// $t was 0, $u array{0}, the result 0|1; now int<0, max>, array{int<0, max>} and int<0, max>

pongPing(static fn (array $u) => $u[0] + 1, static fn (int $t) => [$t], 0); // the same with the parameters reversed
// (float|int), now int<0, max>

ArgumentsHandler::solveClosureAccumulatorTypes() now solves the call's closures together:

  • Accumulators: the templates that some callable parameter passes to its closure and some callable parameter receives back. Either side can be in any of the parameters. GenericParametersAcceptorResolver::getCallableTemplateFlow() reports both sides for each parameter. A single-closure accumulator is the case where both sides are in one parameter.
  • Rounds: the non-closure siblings seed the accumulators. Each round types the participating closures, then joins what they return. The rounds repeat until nothing grows, with the same LOOP_SCOPE_ITERATIONS / GENERALIZE_AFTER_ITERATION widening as Resolve accumulator templates of a closure to a fixed point #7212. Without a fixed point, every closure falls back to gatherClosureArgType() alone, as before.
  • Order inside a round: first a closure whose passed accumulators are all known already, otherwise the next one in processing order. So arguments written against the data flow don't make the solver guess. Without this, pongPing typed g before U was known and leaked float|int into T.
  • Only returns grow: after a closure is typed, only the templates it returns are updated. Its declared parameter (array $u) is an upper bound on U, not a value for it. Joining it in widened U to array.
  • Closure bodies: they are walked with the solution. All solved closure types go into the gathered types at once, so the body of f already sees what g returns.

The C++ port in turbo-ext/src/ArgumentsHandler.cpp mirrors the solver. getCallableTemplateFlow() and resolveAccumulatorTemplateTypes() stay PHP static methods that it calls. resolveAccumulatorTemplateTypes() now takes the accumulator names instead of one parameter, and skips an accumulator the arguments don't determine instead of returning null. Neither method is @api.

Tests:

  • New cases in nsrt/template-accumulator-fixed-point.php. Without the source change, four of their assertions fail.
  • A control that converges on its own: with untyped closures, pingPong(0, fn ($t) => [$t], fn ($u) => count($u) + 1) stays 0|2, which is the exact fixed point, so it is not over-widened.
  • The existing single-closure expectations, including bug-7280 and bug-15388, are unchanged.

Checks:

  • The full suite passes with and without the extension.
  • Self-analysis is clean in both modes.
  • side-by-side.php, signature-parity.php and smoke.php pass.

The first commit adds the // lint >= 8.0 comment that bug-7280.php from #7212 was missing. It uses initial:, so RequiredPhpVersionCommentTest fails on 2.3.x without it.

A template a closure argument returns and another one takes, as the T and U
of callable(T): U and callable(U): T passed together, is now solved by the
same fixed point as a single closure's accumulator. Each round types the
closures in the order their data flows, and only a closure's return grows
the templates it returns.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c3c-cedc-7330-a083-cc986590888f
@ondrejmirtes
ondrejmirtes merged commit ef9ff3f into 2.3.x Nov 27, 2026
422 of 425 checks passed
@ondrejmirtes
ondrejmirtes deleted the template-cycle-solver branch November 27, 2026 11:44
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