Describe how the types of consecutive static variables depend on each other - #6619
Merged
Merged
Conversation
… other
The type of each `static` variable is inferred on its own, so a cache
keyed by one variable and holding its value in another loses the link
between them: after `if ($cachedKey !== $key) { $cachedKey = $key;
$cachedValue = compute(); }` the value is still `string|null`, although it
is null only while the key is.
Nothing between the statements of a run of consecutive top-level `static`
statements can change what the previous calls left in their variables, so
right after the run they hold their defaults or one of the states a call
left them in together. The two-pass driver collects those states - at the
end of the body, at returns and explicit throws, and at calls that could
run the function again (anything but a built-in function without a Closure
parameter) - and describes them with conditional expressions between the
run's variables, which StaticVariableHandler adds after the run. The walks
enter the run with them until they converge along with the types, or are
given up.
The conditions and target types take the remainder form MutatingScope::
mergeWith() uses for its guards - what a variable holds in no state that
leaves out the target type, of everything it can hold - so that they cover
the generalized types of a variable that keeps growing, like a counter.
Behind the staticVariablesFromUsages bleeding edge toggle, like the
inference of the types itself.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VpbB99tJfUmeArX74xqvHv
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.
The types of
staticvariables (#6617, bleeding edge) are inferred one by one, so a cache keyed by one variable and holding its value in another loses the link between them (playground):The value is
nullonly while the key is. This PR teaches the static-variable inference to describe that withConditionalExpressionHolders between the variables.How it works
Runs.
StaticVariableInference::getRuns()finds runs of consecutive top-levelstaticstatements declaring two or more inferred variables (one statement with several variables counts too). Nothing between the statements of a run can change what the previous calls left, so right after the run the variables hold their defaults, or one of the states a call left them in together.States. The two-pass driver collects those joint states:
Closure;It skips unreachable scopes (a
nevervariable) and scopes where the variables still hold exactly what the walk entered the run with.Holders. For each type a variable holds in some state, the condition on another variable is what that variable holds in none of the states that leave the type out, taken from everything it can hold. The target type gets the same remainder treatment. This is the remainder form
MutatingScope::mergeWith()uses for its guards, so the holders cover a variable whose type keeps generalizing, like a counter:Fixpoint.
StaticVariableHandleradds the holders after the run's last statement. The walks enter the run with them until the holders converge along with the types. If they don't, they're given up and the walks go on without them.Soundness cases the fixture pins, where the link must not be claimed:
throwbetween them;Verification
tests/PHPStan/Analyser/nsrt/static-variables-conditional-types.phpholds the playground snippet verbatim plus the cases above. It fails before the change for the right reasons.make testsis green with the extension off and loaded. Walk-trace (PHP vs native) is identical on the full corpus and on the fixtures. Side-by-side, smoke and signature parity pass.make phpstanand cs are clean.StaticVariableInference,TemplateArgumentFrame,StatementsHandler,StaticVariableHandler.🤖 Generated with Claude Code
https://claude.ai/code/session_01VpbB99tJfUmeArX74xqvHv