Apply the effects of a closure's by-ref uses where it is invoked - #6610
Merged
Merged
Conversation
ondrejmirtes
force-pushed
the
closure-signatures-from-usages
branch
2 times, most recently
from
September 27, 2026 10:41
02e504d to
e31e14e
Compare
ondrejmirtes
force-pushed
the
closure-byref-uses-at-invocation
branch
from
September 27, 2026 10:53
8176e9f to
3bd8a5e
Compare
ondrejmirtes
marked this pull request as ready for review
September 27, 2026 11:47
Collaborator
|
This pull request has been marked as ready for review. |
A closure written where nothing types it gets a marker per by-ref use (`use (&$x)`). The two-pass observation classifies each such closure: - local: every invocation was seen. At creation nothing runs (an undefined `$x` becomes null), each invocation walks the body from the state of `$x` there and writes the exit types back. The body's rules are reported by one deferred walk at the end of the enclosing body's second pass, entered with the union of `$x` at the invocations (or at the creation when there was none) - so inside the closure `$x` is what it was where the closure ran. - escaped: the value went where it can be invoked at any time (an argument, a return, a yield, a property, a capture by another function-like). The creation-time fixpoint stays, seeded with the invocation states seen, and local invocations run the fixpoint from the current state. While observing, invocations run the fixpoint from the current state so the first pass contains whatever the second one computes; one from within the creation's fixpoint (no throw points, accepted arguments) adds nothing and skips the walk. Captured by-value uses and the check that an invocation runs where the closure was created come from the creation's stored expression result; only the end-of-body collection of the invocation entries flattens the facts. Gated by featureToggles.closureSignaturesFromUsages. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VpbB99tJfUmeArX74xqvHv
… function The reproducers of both issues are top-level code, where the two-pass observation does not run; the tests wrap them in a function. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VpbB99tJfUmeArX74xqvHv
ondrejmirtes
force-pushed
the
closure-byref-uses-at-invocation
branch
from
September 27, 2026 11:48
3bd8a5e to
6e1ba36
Compare
This was referenced Sep 27, 2026
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 with
use (&$x)is analysed today once, at its creation, with a fixpoint entry:$xis the union of every state any number of runs could leave, both inside the body and outside from the creation on. The type inside the closure is therefore too wide when the closure runs once from a known state, and stale when$xchanges between creation and invocation.This PR reuses the two-pass observation from #6332 and #6604 (merged) to follow where the closure runs:
How it works
byRefUseTypesonClosureType). The observation classifies each closure:$xbecomesnull.$xthere and writes back the exit types and the throw points.$xat the invocations, or the creation state when there was none.$xat the local call.catchcould see mid-run states).@template, top-level code and non-closed bodies (the two-pass driver doesn't run there).featureToggles.closureSignaturesFromUsages. Native twins are ported in the same commit.Verification
closure-byref-uses-at-invocation.php(inside and after the closure: once, twice, in loops, conditional, never invoked, modified in between, undefined, by-value captures, accumulators, catch, escapes, invocations beforethrow/return/break), a toggle-off fixture, and a rule test. Several existing fixtures pinned the old fixpoint types and got more precise.make phpstanand cs. Side-by-side, signature parity, smoke (with a new by-ref frame differential) pass; walk-trace is identical.ifs: at most +18%Issues
Both reproducers are top-level code, where the two-pass observation doesn't run. The regression tests (
nsrt/bug-7751.php,nsrt/bug-14248.php,StrictComparisonOfDifferentTypesRuleTest::testBug14248) wrap them in a function; they fail on #6604.Closes phpstan/phpstan#7751
Closes phpstan/phpstan#14248
🤖 Generated with Claude Code
https://claude.ai/code/session_01VpbB99tJfUmeArX74xqvHv
A closure with by-ref uses is left out of #6604's per-invocation return types: what it returns depends on its by-ref variables where it runs, which the invocation walk here follows.