Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes phpstan/phpstan#15757
ClosureTypeResolver::getClosureType()walked the closure body on an emptyExpressionResultStorage, 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)is2, not the return type of the joined signatureClosure(0..9): 0..18. In the empty storage it found nothing, so:$doubleFn(1) = 2, andUwas inferred as-2..7;getClosureType(), called fromArgumentsHandler::gatherClosureArgType()and again whenCallToFunctionParametersRuleasks for the argument's type, typed it from0..18and producedClosure(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. Theturbo-extmirror inClosureTypeResolver.cppgets 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, whilegetClosureType()caches closure types by scope, parameter types and template frame only. I found no remaining failing case:match,switch,?:,??andforeach, 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 servesfindAssignedClosures(), carry the creation scope of the closures it resolved, so invocations no longer depend on the storage. That touches the frame,FuncCallHandlerandClosureSignatureInferencealong with their C++ mirrors, so I kept it out of this fix.The error needs both
closureSignaturesFromUsagesandunresolvedTemplateArguments. 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-loopforeachiteratee): each sees$doubleFn(1)as2.Verification:
CallToFunctionParametersRuleTest::testBug15757()(the issue's playground snippet) andnsrt/bug-15757.phpfail without the fix (the reportedargument.typeerror, and0|2|…|18instead of2for a closure and an arrow function passed to acallable(): Tparameter) and pass with it. They also pass with the extension loaded and only the C++ change in place.make phpstanandmake csreport no errors.smoke.phpandsignature-parity.phppass. Not run:make lint-turbo(clang-tidy is not installed here).🤖 Generated with Claude Code (Opus 5.5)