Skip to content

Walk a closure body for its type on a duplicate of the caller's storage - #7093

Open
gnutix wants to merge 1 commit into
phpstan:2.3.xfrom
gnutix:fix/bug-15757
Open

gnutix wants to merge 1 commit into
phpstan:2.3.xfrom
gnutix:fix/bug-15757

Conversation

@gnutix

@gnutix gnutix commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes phpstan/phpstan#15757

ClosureTypeResolver::getClosureType() walked the closure body on an empty ExpressionResultStorage, even when its caller passed the storage of the analysis in progress. FuncCallHandler::resolveInvokedClosureType() looks up the creation result of a closure whose signature is inferred from its usages in that storage, to type an invocation with narrower arguments from the body: $doubleFn(1) is 2, not the return type of the joined signature Closure(0..9): 0..18. In the empty storage it found nothing, so:

  • the walk of the argument typed the arrow function from $doubleFn(1) = 2, and U was inferred as -2..7;
  • getClosureType(), called from ArgumentsHandler::gatherClosureArgType() and again when CallToFunctionParametersRule asks for the argument's type, typed it from 0..18 and produced Closure(int): (-18..9).

The rule then compared two different types of the same arrow function. The walk now runs on $storage->duplicate(), as on-demand processing already does (MutatingScope::resolveTypeOfNewWorldHandlerNode(), NodeScopeResolver::processSyntheticOnDemand()): the walk's own results stay in the duplicate, and the closure's creation outside the body resolves through the fallback. The turbo-ext mirror in ClosureTypeResolver.cpp gets the same change.

Why this approach: every caller of getClosureType() already passes the storage of the analysis in progress, and the walk discarded it. Walking on a duplicate keeps what the caller handed over without letting the walk's results leak back. It is the pattern on-demand processing uses, a two-line change on each side, and it leaves alone the rule, the template inference and the signature inference. It does not remove the underlying fragility: resolveInvokedClosureType() types an invocation from a storage lookup, so the same expression can get different types depending on which storage a walk runs in, while getClosureType() caches closure types by scope, parameter types and template frame only. I found no remaining failing case: match, switch, ?:, ?? and foreach, which still process on a fresh storage, answer from the scope here. A sturdier fix would have the template-argument frame, which is on the scope and already serves findAssignedClosures(), carry the creation scope of the closures it resolved, so invocations no longer depend on the storage. That touches the frame, FuncCallHandler and ClosureSignatureInference along with their C++ mirrors, so I kept it out of this fix.

The error needs both closureSignaturesFromUsages and unresolvedTemplateArguments. Turning off either one makes it go away, and without bleedingEdge the code analyses cleanly. The issue says 2.3.5 did not report it, but 2.3.5 built from source reproduces it with bleedingEdge here, so the cause predates 2.3.6. It is not the remembered-call merging from #7064.

The same pattern does not reproduce at the other sites that process an expression on a fresh storage (the remaining type of match/switch, ?:, ??, the post-loop foreach iteratee): each sees $doubleFn(1) as 2.

Verification:

  • CallToFunctionParametersRuleTest::testBug15757() (the issue's playground snippet) and nsrt/bug-15757.php fail without the fix (the reported argument.type error, and 0|2|…|18 instead of 2 for a closure and an arrow function passed to a callable(): T parameter) and pass with it. They also pass with the extension loaded and only the C++ change in place.
  • The full test suite passes with and without the extension (23913 tests). make phpstan and make cs report no errors.
  • Extension: the strict build, smoke.php and signature-parity.php pass. Not run: make lint-turbo (clang-tidy is not installed here).

🤖 Generated with Claude Code (Opus 5.5)

ClosureTypeResolver::getClosureType() walked the body on an empty
ExpressionResultStorage, even when the caller handed it the storage of the
analysis in progress. An invocation of a closure whose signature is
inferred from its usages (closureSignaturesFromUsages) finds the closure's
creation result in that storage to return what the body returns for
narrower arguments. With an empty storage it found nothing, so the walk
typed `$doubleFn(1)` as the joined signature's return type (`0..18`) while
the walk of the argument itself typed it as `2`. The callable parameter
check then compared two different types of the same arrow function.

The walk now runs on a duplicate of the given storage, as on-demand
processing does: its own results stay in the duplicate, and nodes outside
the body resolve through the fallback. The turbo-ext mirror gets the same
change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Closure passed to a callable parameter is typed from a previous call of a captured closure (bleedingEdge, 2.3.6)

1 participant