Skip to content

Forget narrowed mutable properties in closures that may run later - #7263

Merged
ondrejmirtes merged 4 commits into
2.3.xfrom
closure-captured-property-narrowing
Dec 3, 2026
Merged

ondrejmirtes merged 4 commits into
2.3.xfrom
closure-captured-property-narrowing

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

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:

if ($holder->value === null) {
	return 0;
}
$length = fn (): int => strlen($holder->value);
$holder->value = null;
return $length();

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, for MutatingScope::getType(), on-demand argument types in ArgumentsHandler, FuncCallHandler, ClosureParameterResolver and the by-reference use fixpoint. Those callers now ask ClosureProcessor::getClosureType(). ClosureTypeResolver no 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:

  • immediately invoked: (fn () => ...)()
  • passed to a parameter that calls it during the call (array_map, usort, @param-immediately-invoked-callable), decided like ArgumentsHandler::callCallbackImmediately(). An untagged method parameter counts as later-invoked, so such methods need @param-immediately-invoked-callable to keep the narrowing.

ArgumentsHandler marks callback arguments for that check. Closures now keep narrowed properties of $this on entry like arrow functions do, so both give the same result (#12912, #13563, and bug-13321.php for an immediately invoked closure).

RecursionGuard::run() and runOnObjectIdentity() 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 unresolvedTemplateArguments toggle, 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

@ondrejmirtes
ondrejmirtes force-pushed the closure-captured-property-narrowing branch from 7382515 to 4c3ad2d Compare December 3, 2026 12:10
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
ondrejmirtes force-pushed the closure-captured-property-narrowing branch from 33f3a64 to a3583db Compare December 3, 2026 13:30
@ondrejmirtes
ondrejmirtes merged commit 80b3c8c into 2.3.x Dec 3, 2026
121 of 131 checks passed
@ondrejmirtes
ondrejmirtes deleted the closure-captured-property-narrowing branch December 3, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment