-
Notifications
You must be signed in to change notification settings - Fork 582
Recover never-collapsed variables from the loop-condition falsey scope for side-effecting while/for conditions
#5923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phpstan-bot
wants to merge
6
commits into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-7a1yw80
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
292061b
Recover never-collapsed variables from the loop-condition falsey scop…
staabm 150cf4d
Add rule test covering the reported identical.alwaysFalse false positive
phpstan-bot 76d7aed
fix name collision
staabm 5ae4e85
use 1:1 repro
staabm 4dec75f
Recover short-circuited side-effecting loop variables from the condit…
phpstan-bot c9af731
Add do-while assertions to the side-effecting loop-condition test
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| <?php | ||
|
|
||
| namespace Bug10109b; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function simple(): void | ||
| { | ||
| $x = 5; | ||
| while (--$x > 0) { | ||
| } | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function withBody(): void | ||
| { | ||
| $x = 5; | ||
| while (--$x > 0) { | ||
| echo $x; | ||
| } | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function preIncrement(): void | ||
| { | ||
| $x = 5; | ||
| while (++$x < 10) { | ||
| } | ||
|
|
||
| assertType('int<10, max>', $x); | ||
| } | ||
|
|
||
| function assignInCondition(): void | ||
| { | ||
| $x = 5; | ||
| while (($x = $x - 1) > 0) { | ||
| } | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function postDecrement(): void | ||
| { | ||
| $x = 5; | ||
| while ($x-- > 0) { | ||
| } | ||
|
|
||
| assertType('int<min, -1>', $x); | ||
| } | ||
|
|
||
| function forLoop(): void | ||
| { | ||
| for ($x = 5; --$x > 0;) { | ||
| } | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function shortCircuitedSideEffect(): void | ||
| { | ||
| // The side effect is short-circuited, so the loop can also exit with $x unchanged. The | ||
| // after-loop type must still include the values produced by the decrement (notably 0). | ||
| $x = 5; | ||
| while (mt_rand(0, 10) < 10 && --$x > 0) { | ||
| } | ||
|
|
||
| assertType('int<min, 5>', $x); | ||
| } | ||
|
|
||
| function shortCircuitedCounter(): void | ||
| { | ||
| // A post-increment counter keeps its precise in-loop bound instead of being widened by the | ||
| // loop-condition falsey scope. | ||
| $i = 0; | ||
| while (mt_rand(0, 10) < 10 && $i++ < 10) { | ||
| } | ||
|
|
||
| assertType('int<0, 10>', $i); | ||
| } | ||
|
|
||
| function noSideEffectInCondition(): void | ||
| { | ||
| $x = 5; | ||
| while ($x > 0) { | ||
| $x = $x - 1; | ||
| } | ||
|
|
||
| assertType('0', $x); | ||
| } | ||
|
|
||
| function doWhileDecrement(): void | ||
| { | ||
| // do-while evaluates its condition once at the bottom (via getFalseyScope()), so the side | ||
| // effect is applied exactly once and the after-loop type is already sound without recovery. | ||
| $x = 5; | ||
| do { | ||
| } while (--$x > 0); | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function doWhilePreIncrement(): void | ||
| { | ||
| $x = 5; | ||
| do { | ||
| } while (++$x < 10); | ||
|
|
||
| assertType('int<10, max>', $x); | ||
| } | ||
|
|
||
| function doWhileAssignInCondition(): void | ||
| { | ||
| $x = 5; | ||
| do { | ||
| } while (($x = $x - 1) > 0); | ||
|
|
||
| assertType('int<min, 0>', $x); | ||
| } | ||
|
|
||
| function doWhilePostDecrement(): void | ||
| { | ||
| $x = 5; | ||
| do { | ||
| } while ($x-- > 0); | ||
|
|
||
| assertType('int<min, -1>', $x); | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug10109; | ||
|
|
||
| // simpler | ||
| $x = 5; | ||
| while (--$x > 0) { | ||
| echo "$x\n"; | ||
| } | ||
| if ($x === 0) { | ||
| echo "zero\n"; | ||
| } | ||
|
|
||
| // closer to real codebase | ||
| $x = 5; | ||
| while (mt_rand(0, 10) < 10 && --$x > 0) { | ||
| echo "$x\n"; | ||
| } | ||
| if ($x === 0) { | ||
| echo "zero\n"; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also test
do {} while()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've addressed staabm's review request to also test
do {} while().What I added
Four
do-whilevariants totests/PHPStan/Analyser/nsrt/bug-10109b.php, mirroring the existingwhilecases:do {} while (--$x > 0)→int<min, 0>do {} while (++$x < 10)→int<10, max>do {} while (($x = $x - 1) > 0)→int<min, 0>do {} while ($x-- > 0)→int<min, -1>Why the expectations are what they are
do-whilewas never affected by the original bug: it evaluates its condition once at the bottom viaprocessExprNode(...)->getFalseyScope(), so the side effect is applied exactly once and there is no contradiction between the in-body value and the narrowed exit value. I verified empirically with\PHPStan\dumpTypethat PHPStan already infers these types correctly — therestoreLoopConditionSideEffectsrecovery path doesn't need to fire fordo-while. The new assertions lock in that sound behavior and document the contrast withwhile/for.Verification
NodeScopeResolverTest(which loadsnsrt/bug-10109b.php): 1679/1679 green.make phpstan: no errors.Committed as
c9af7312dand pushed.