Skip to content
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

Maintain loop start val after increment/decrement #10263

Merged

Conversation

robchett
Copy link
Contributor

@robchett robchett commented Oct 8, 2023

Fixes #10213

@robchett robchett force-pushed the maintain_loop_start_val_after_increment branch 5 times, most recently from 72a8576 to 9debd81 Compare October 9, 2023 20:20
@orklah
Copy link
Collaborator

orklah commented Oct 16, 2023

(Note that I've not commented on this yet because there were errors on unit tests and I assumed you were still working on it :) )

@robchett
Copy link
Contributor Author

Yes, two steps forwards, one step back. Still working out the intricacies of the loopAnalyzer

@robchett robchett force-pushed the maintain_loop_start_val_after_increment branch from 9debd81 to 0fea1a3 Compare October 27, 2023 11:54
@robchett robchett changed the base branch from 5.x to master October 27, 2023 11:54
@robchett robchett force-pushed the maintain_loop_start_val_after_increment branch from f6a556e to 0fea1a3 Compare October 27, 2023 11:56
@robchett
Copy link
Contributor Author

@orklah the lower bound is being properly detected.

I had to modify a test tests/Loop/ForTest.php
as it now shows an error which was a false-negative before. The continue statement was incorrectly suppressing an list-key offset if the form was changed very slightly. https://psalm.dev/r/303d48a67b shows it allowing list access via a negative key. It's not 100% as it hasn't reconciled the $i > 0 and $i-- to int<0, 20> before checking the array offset. I think the false positive is better than the false negative?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/303d48a67b
<?php
/**
 * @param list<int> $arr
 */
function cartesianProduct(array $arr) : void {
    for ($i = 20; $arr[$i] === 5 && $i >= -10; $i--) {
        /** @psalm-trace $i */
    }
}
Psalm output (using commit 147505c):

INFO: Trace - 7:0 - $i: int<-10, max>

@robchett robchett force-pushed the maintain_loop_start_val_after_increment branch from 0fea1a3 to fecc4eb Compare November 3, 2023 21:42
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 4, 2023
@orklah
Copy link
Collaborator

orklah commented Nov 4, 2023

Thanks! Seems like an improvement :)

@orklah orklah merged commit 7233f38 into vimeo:master Nov 4, 2023
39 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lower boundary of for loop variable is not detected correctly
2 participants