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

5.4: Issue with flow analysis causes NoValue false positive #8952

Open
pilif opened this issue Dec 20, 2022 · 9 comments
Open

5.4: Issue with flow analysis causes NoValue false positive #8952

pilif opened this issue Dec 20, 2022 · 9 comments
Labels

Comments

@pilif
Copy link
Contributor

pilif commented Dec 20, 2022

https://psalm.dev/r/8a407af77c

psalm seems to get confused that $v is updated in the loop after the condition.

This was fine in 5.3 and prior and is broken in 5.4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8a407af77c
<?php

$v = 0;
do {
    $foo = "a";
    if ($v > 0) {
        $foo .= sprintf('_%02d', $v);
    }
    $v++;
} while (rand(1,10) < 5);
echo $foo;
Psalm output (using commit 62db5d4):

ERROR: NoValue - 7:34 - All possible types for this argument were invalidated - This may be dead code

@pilif pilif changed the title Issue with flow analysis causes NoValue false positive 5.4: Issue with flow analysis causes NoValue false positive Dec 20, 2022
@danog
Copy link
Collaborator

danog commented Dec 20, 2022

Encountered this too when making a PR, possibly caused by #8934

The issue is that the if statement is analyzed twice, once with the impossible values inferred on the first iteration (always never), then on future iterations which are not never: https://psalm.dev/r/3b6e234333

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/3b6e234333
<?php

$v = 0;
do {
    $foo = "a";
    if ($v > 0) {
        /** @psalm-trace $v */;
        $foo .= sprintf('_%02d', $v);
    }
    $v++;
} while (rand(1,10) < 5);
echo $foo;
Psalm output (using commit 62db5d4):

INFO: Trace - 7:31 - $v: never

ERROR: NoValue - 8:34 - All possible types for this argument were invalidated - This may be dead code

INFO: Trace - 7:31 - $v: int<1, max>

@pilif
Copy link
Contributor Author

pilif commented Dec 20, 2022

in my case, the workaround is to put the increment before the if and change the condition to > 1. Which is also strange because that should IMHO fail the same way, but it doesn't.

@danog
Copy link
Collaborator

danog commented Dec 20, 2022

Yeah, in the case of https://psalm.dev/r/b33c14eaf6 there should be a paradox since $v is always > 0, there's already an open issue for this: #8059.

Overall I think the current behavior for https://psalm.dev/r/8a407af77c is correct, as it detects that there might be an impossible code path at some point in the iteration, closing this unless someone else comes up with a more broken example.

@danog danog closed this as completed Dec 20, 2022
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b33c14eaf6
<?php

$v = 0;
do {
    $foo = "a";
    $v++;
    /** @psalm-trace $v */;
    if ($v > 0) {
        $foo .= sprintf('_%02d', $v);
    }
} while (rand(1,10) < 5);
echo $foo;
Psalm output (using commit 62db5d4):

INFO: Trace - 7:27 - $v: int<1, max>
https://psalm.dev/r/8a407af77c
<?php

$v = 0;
do {
    $foo = "a";
    if ($v > 0) {
        $foo .= sprintf('_%02d', $v);
    }
    $v++;
} while (rand(1,10) < 5);
echo $foo;
Psalm output (using commit 62db5d4):

ERROR: NoValue - 7:34 - All possible types for this argument were invalidated - This may be dead code

@danog
Copy link
Collaborator

danog commented Dec 20, 2022

cc @orklah just in case you want to comment on this too

@orklah
Copy link
Collaborator

orklah commented Dec 20, 2022

The issue is that the if statement is analyzed twice, once with the impossible values inferred on the first iteration (always never), then on future iterations which are not never: https://psalm.dev/r/3b6e234333

I agree with that, so it seems there is indeed a bug here. I would keep this open? Unless I missed something?

@orklah orklah reopened this Dec 20, 2022
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/3b6e234333
<?php

$v = 0;
do {
    $foo = "a";
    if ($v > 0) {
        /** @psalm-trace $v */;
        $foo .= sprintf('_%02d', $v);
    }
    $v++;
} while (rand(1,10) < 5);
echo $foo;
Psalm output (using commit 62db5d4):

INFO: Trace - 7:31 - $v: never

ERROR: NoValue - 8:34 - All possible types for this argument were invalidated - This may be dead code

INFO: Trace - 7:31 - $v: int<1, max>

@orklah orklah added the bug label Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants