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

Psalm fails to narrow type to positive int inside loop #8865

Closed
theodorejb opened this issue Dec 8, 2022 · 3 comments · Fixed by #8934
Closed

Psalm fails to narrow type to positive int inside loop #8865

theodorejb opened this issue Dec 8, 2022 · 3 comments · Fixed by #8934

Comments

@theodorejb
Copy link
Contributor

Psalm fails to infer that a variable is a positive int when the condition occurs inside a loop.

https://psalm.dev/r/155977bb20

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/155977bb20
<?php

function sleepLoop(float $seconds, int $repeat): void
{
    $microseconds = (int) ($seconds * 1_000_000);

    for ($i = 0; $i < $repeat; $i++) {
        if ($microseconds > 0) {
            usleep($microseconds);
        }   
    }
}

sleepLoop(1.5, 5);
Psalm output (using commit 18011eb):

ERROR: ArgumentTypeCoercion - 9:20 - Argument 1 of usleep expects int<0, max>, but parent type int provided

@theodorejb theodorejb changed the title Incorrect type inference inside loop Psalm fails to narrow type to positive int inside loop Dec 9, 2022
@theodorejb
Copy link
Contributor Author

Note: this worked as expected in Psalm 4.x, so it's a regression in Psalm 5.

@orklah
Copy link
Collaborator

orklah commented Dec 18, 2022

Note:
Following tests are still broken:
SKIPPED-intLoopNegative
SKIPPED-statementsInLoopAffectsEverything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants