Repository navigation
Forget narrowed mutable properties in closures that may run later - #7263
Merged
Merged
Conversation
ondrejmirtes
force-pushed
the
closure-captured-property-narrowing
branch
from
December 3, 2026 12:10
7382515 to
4c3ad2d
Compare
A closure or arrow function sees the objects it captures by handle, so a property narrowed where the closure is created can be reassigned before it runs. Closures kept the narrowed type of properties fetched on their by-value uses and arrow functions kept every narrowed expression, so `fn () => strlen($holder->value)` was accepted after `$holder->value !== null` even when `$holder->value = null` ran before the call. Both now drop narrowed expressions (and, for arrow functions, the conditional narrowings) that fetch a property which is not readonly, unless the function runs where it is written: invoked immediately, or passed to a parameter that calls it during the call - decided like ArgumentsHandler::callCallbackImmediately(). CallbackArgVisitor marks closures written as call arguments for that check. RecursionGuard::run() and runOnObjectIdentity() call their callback during the call, which their PHPDoc now says. Amp-Thread-ID: https://ampcode.com/threads/T-01a22b9a-ea57-769d-966d-00063668bb3b
…he call
The issue bot found these reproducers changed by forgetting narrowed
mutable properties in closures that may run later: an arrow function now
sees the same property type as the equivalent closure, and a captured
object's property narrowed to array{} is no longer empty inside a closure
invoked after the property changed.
Amp-Thread-ID: https://ampcode.com/threads/T-01a22b9a-ea57-769d-966d-00063668bb3b
ClosureTypeResolver::getClosureType() walked a closure or arrow function body on its own, with its own scope entry and depth guard, for MutatingScope::getType(), on-demand argument types, FuncCallHandler, ClosureParameterResolver and the by-reference use fixpoint. Those callers now ask ClosureProcessor::getClosureType(), so ClosureProcessor is the only code that walks a body; ClosureTypeResolver returns the declared signature, reads cached types and builds them from a walk. Every walk enters through ClosureProcessor::enterWhereInvoked(), which forgets narrowed mutable properties unless the function runs where it is written: invoked immediately, or passed to a parameter that calls it during the call, decided like callCallbackImmediately(). ArgumentsHandler marks callback arguments for it, replacing CallbackArgVisitor. Closures now keep narrowed properties of $this on entry like arrow functions, so both get the same result. The observation pass over a function body runs outside bleeding edge too. Its frame resolves template arguments, infers closure signatures and static variables only with the unresolvedTemplateArguments toggle. Amp-Thread-ID: https://ampcode.com/threads/T-01a22b9a-ea57-769d-966d-00063668bb3b
ondrejmirtes
force-pushed
the
closure-captured-property-narrowing
branch
from
December 3, 2026 13:30
33f3a64 to
a3583db
Compare
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.
A closure or arrow function sees captured objects by handle, so a property narrowed where the closure is created can be reassigned before it runs. Closures kept the narrowed type of properties fetched on their by-value uses, and arrow functions kept every narrowed expression, so this passed while PHP throws a
TypeError:ClosureProcessor is the only code that walks a closure body
ClosureTypeResolver::getClosureType()used to walk the body itself, with its own scope entry and depth guard, forMutatingScope::getType(), on-demand argument types inArgumentsHandler,FuncCallHandler,ClosureParameterResolverand the by-reference use fixpoint. Those callers now askClosureProcessor::getClosureType().ClosureTypeResolverno longer walks: it returns the declared signature (getShallowClosureType()), reads cached types and builds them from a walk.One capture rule for closures and arrow functions
Every walk enters through
ClosureProcessor::enterWhereInvoked(). It forgets narrowed properties that are not readonly unless the function runs where it is written:(fn () => ...)()array_map,usort,@param-immediately-invoked-callable), decided likeArgumentsHandler::callCallbackImmediately(). An untagged method parameter counts as later-invoked, so such methods need@param-immediately-invoked-callableto keep the narrowing.ArgumentsHandlermarks callback arguments for that check. Closures now keep narrowed properties of$thison entry like arrow functions do, so both give the same result (#12912, #13563, andbug-13321.phpfor an immediately invoked closure).RecursionGuard::run()andrunOnObjectIdentity()call their callback during the call and say so in their PHPDoc.Observation pass
The observation pass over a function body now always runs. Its frame resolves template arguments, infers closure signatures and static variables only with the
unresolvedTemplateArgumentstoggle, so bidirectional type narrowing stays bleeding edge only.The native twins mirror every change; side-by-side, signature parity, smoke and the full suite with the extension loaded pass.
Closes phpstan/phpstan#15891
Closes phpstan/phpstan#12912
Closes phpstan/phpstan#13563
Closes phpstan/phpstan#10345
Closes phpstan/phpstan#11394