Repository navigation
Do not project narrowings of expressions reading the reassigned variable into conditional expressions guarded by it - #7090
Closed
phpstan-bot wants to merge 1 commit into
Conversation
…ble into conditional expressions guarded by it
- AssignHandler::isExprSafeToProjectThroughVariable() now rejects any
expression that reads the variable being assigned. For
`$s = strlen($s ?: "") ?: 0` the narrowings of `$s ?: ""` and
`strlen($s ?: "")` were recorded as "if $s=int<1, max> then
strlen($s ?: "") is int<1, max>". Those expressions describe the old $s,
so the holders were stale. They were added after assignVariable() had
already invalidated the expressions, so they survived.
- The stale holders caused the hang. A rule priced the assignment on the
execution-end scope, which processed it on demand. The inner ternary's
branches fired the holders, so `strlen($s ?: "")` differed between
them. MutatingScope::findUninformativeMergedCalls() then priced the
call again on demand, which walked the same ternary again, with no end.
- The same stale projection also gave wrong types without a hang:
`$f = $f->p !== null ? $f->next : null; if ($f !== null) { $f->p }`
inferred `int` instead of `int|null`, as did the `=== null` form.
- Ported the same check to turbo-ext/src/AssignHandler.cpp, using the
shared findFirst walker.
- Probed `match`-assigned values, `??`, and `isset($a['x'])`-based ternaries
that reassign the read variable. Those were already correct (match arm
scopes go through assignVariable() and its invalidation).
Contributor
|
I like #7088 more |
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.
Summary
Analysis never finished on
$s = strlen($s ?: "") ?: 0;(and onpreg_split(..., $x ?: '', ...) ?: []assigned back to$x). This was a regression in 2.3.6. When a variable is assigned,AssignHandlerturns narrowings of parts of the assigned expression into conditional expressions guarded by that variable. It also did this for parts that read the variable itself, likestrlen($s ?: ""). Those parts describe the variable's old value, so the guards were wrong. They led to infinite on-demand re-processing. These parts are now skipped.Changes
src/Analyser/ExprHandler/AssignHandler.php:isExprSafeToProjectThroughVariable()returnsfalsefor any expression that contains a read of the assigned variable (NodeFinder::findFirst()for aVariablewith that name).turbo-ext/src/AssignHandler.cpp: the same check in the native mirror, through the sharedpt_find_first_recursive()walker. The differential smoke test prints ALL OK, and analysis output is identical with and without the extension.$f = $f->p !== null ? $f->next : null;and$f = $f->p === null ? null : $f->next;used to narrow$f->pof the new$ftointafterif ($f !== null).matchassigned back into a variable it reads (its arm scopes go throughassignVariable(), which invalidates them),$f = $f->next ?? null, and$a = isset($a['x']) ? ... : ....Root cause
Before the assignment, the narrowings of the assigned expression's parts are recorded as conditional holders like "if
$sisint<1, max>thenstrlen($s ?: "")isint<1, max>".assignVariable()drops remembered expressions containing$s, but these holders are added afterwards, so they survived even though they talk about the previous$s.In the reported case,
ParameterOutExecutionEndTypeRuleasked for the type of the assignment on the execution-end scope. Because the variable state differs there, the assignment was processed on demand. The inner$s ?: ""ternary narrowed the new$s, so the stale holders fired, andstrlen($s ?: "")got different types in the two branches. The merge then askedMutatingScope::findUninformativeMergedCalls()for the call's own type. Since the call was still being processed, that triggered another on-demand walk of the same call and ternary, recursing without end. With the stale holders gone, the branches no longer differ and the merge doesn't re-enter.Test
tests/PHPStan/Analyser/nsrt/bug-15754.phpcontains:preg_split()variant from the Rector report$f = $f->p ... $f->nextcases that inferredintinstead ofint|nullWithout the fix, the test hangs (killed by timeout). With the fix, it passes both with and without the turbo extension.
Fixes phpstan/phpstan#15754