Repository navigation
Conversation
The convergence passes walk the body in deep context, where a nested loop is walked once instead of to its fixpoint. When the back edge is only reached after the nested loop has iterated more than once, the pass finds no back edge and stopped with the counter still at its initial value. The final walk then saw `$i` as `0` and reported `$i < 10` as always true. A dead back edge is still trusted when the body is replayable - the deep walk is then the same as the final one. Otherwise the pass continues from the end of the body, as 2.2 did. Closes phpstan/phpstan#15420
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.
Closes phpstan/phpstan#15420
Since 2.3.0, a
forloop whose body only loops back after a nested loop has iterated more than once is analysed as if it ran exactly once. The loop condition is then reported as always true:ForHandler's convergence passes walk the body in deep context, where the innerforeachruns its body once and does not converge. In that walk$seenis stillarray{}, soarray_key_exists()is always false,$laterstays[]and the body always reachesbreak.getLoopBackEdgeScope()returns null and the pass stopped with$bodyScope = $prevScope, so$i++was never applied and$istayed0. The final top-level walk does converge the innerforeachand finds the back edge, but by then the condition had already been priced with$ias0.2.2.17 continued from the end of the body in every pass, so the pass reached
$i++and the counter widened toint<0, 9>. Going back to that everywhere would undo the precision 2.3 gained for loops that really never loop back (for-loop-i-type.phpexpects1afterfor ($i = 1; $i < 50; $i++) { break; }, and 2.2.17 gaveint<1, max>). This PR therefore usesisReplayableConvergenceBody()to tell the two cases apart:ForHandlercontinues from$bodyScopeResult->getScope(), as 2.2.17 did.turbo-ext/src/ForHandler.cppgets the same change, andTurboExtensionEnabler::EXPECTED_EXTENSION_VERSIONis bumped in a separate commit.tests/PHPStan/Rules/Comparison/data/bug-15420.phpis the issue's code unchanged. The test fails without the change inForHandler.php(it reports thesmaller.alwaysTrueerror on line 9) and passes with it.I left
WhileHandler,DoWhileHandlerandForeachHandleralone. They stop early in the same way, but I could not find a case that 2.2.17 analysed correctly and 2.3.x does not. For example, awhileloop with the$i++after thebreakcheck reports the same false positive on 2.2.17, because the deep walk never reaches the increment. I can look into those in a separate PR if you want.Verified:
make testspasses with and without the turbo extension loaded (built withprint-warn-flags).turbo-ext/tests/smoke.phpandsignature-parity.phppass.