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

IfAnalyzer incorrectly updates outer scope #9433

Closed
ptomulik opened this issue Feb 28, 2023 · 10 comments
Closed

IfAnalyzer incorrectly updates outer scope #9433

ptomulik opened this issue Feb 28, 2023 · 10 comments

Comments

@ptomulik
Copy link
Contributor

ptomulik commented Feb 28, 2023

Consider this example, which correctly reports ReferenceConstraintViolation (dumb inserts into list may introduce discontinuites in indexint turning list into array):

https://psalm.dev/r/461cbad1b2

Now, adding dummy if(isset(...)){}, which has no chance to change flow of control, results with false negative (error disappears):

https://psalm.dev/r/dd0484a239

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/461cbad1b2
<?php

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    $list[$offset] = $val;
}
Psalm output (using commit 381a23b):

ERROR: ReferenceConstraintViolation - 6:24 - Variable $list is limited to values of type list<string> because it is passed by reference, non-empty-array<int, string> type found. Use @param-out to specify a different output type
https://psalm.dev/r/dd0484a239
<?php

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    if(isset($list[$offset])) { }
    $list[$offset] = $val;
}
Psalm output (using commit 381a23b):

No issues!

@ptomulik
Copy link
Contributor Author

Handling list assignments is more interesting, just compare these two examples (and the very first example without if(isset(...)):

https://psalm.dev/r/7310523657

https://psalm.dev/r/14d1091480

The difference between above two is isset() vs !isset().

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    if(isset($list[$offset])) {
        $list[$offset] = $val;
    }   
}
Psalm output (using commit 381a23b):

No issues!
https://psalm.dev/r/14d1091480
<?php

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    if(!isset($list[$offset])) {
        $list[$offset] = $val;
    }   
}
Psalm output (using commit 381a23b):

No issues!

@orklah
Copy link
Collaborator

orklah commented Feb 28, 2023

Note: you can use @psalm-trace to see what Psalm think of the types at different places:
https://psalm.dev/r/c3879d8df6
https://psalm.dev/r/f7570574b5

So the difference there is Psalm seems to be convinced that the type is still a list but only after isset() is checked. According to your other snippets, it seems it doesn't even matter how isset was checked (checking the existence or absence do the same)

My guess is that there's a flaw in the way Psalm adds a dynamic offset to an existing list. It may be hard to solve, sometimes, Psalm makes some simplifications in order to allow a specific code construct but this creates edge cases like this

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    if(isset($list[$offset])) { 
    	/** @psalm-trace $list */;
    }
    /** @psalm-trace $list */;
    $list[$offset] = $val;
    /** @psalm-trace $list */;
}
Psalm output (using commit 381a23b):

INFO: Trace - 9:31 - $list: list<string>

INFO: Trace - 11:30 - $list: list<string>

INFO: Trace - 13:30 - $list: non-empty-list<string>
https://psalm.dev/r/f7570574b5
<?php

/**
 * @param list<string> $list
 */
function modify(array &$list, int $offset, string $val): void
{
    /** @psalm-trace $list */;
    $list[$offset] = $val;
    
    /** @psalm-trace $list */;
}
Psalm output (using commit 381a23b):

INFO: Trace - 8:30 - $list: list<string>

INFO: Trace - 11:30 - $list: non-empty-array<int, string>

ERROR: ReferenceConstraintViolation - 6:24 - Variable $list is limited to values of type list<string> because it is passed by reference, non-empty-array<int, string> type found. Use @param-out to specify a different output type

@ptomulik
Copy link
Contributor Author

For the example with if(isset(...)) { }, the contents of outer_context changes here:

@orklah
Copy link
Collaborator

orklah commented Feb 28, 2023

yeah but potentially, you want this context to change (for example if you change the variable inside of a branch of the if or if one of the branch returns or something like that)

ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 1, 2023
@ptomulik
Copy link
Contributor Author

ptomulik commented Mar 1, 2023

yeah but potentially, you want this context to change (for example if you change the variable inside of a branch of the if or if one of the branch returns or something like that)

sure, but it should be done correctly, currently it's buggy, the problem appears to be in another place, so that 289 line is only a starting point for deeper search. Anyway, the fix is on my way.

ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 1, 2023
ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 1, 2023
@ptomulik ptomulik mentioned this issue Mar 1, 2023
@ygottschalk
Copy link
Contributor

possibly related or same issue: #9412

ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 1, 2023
ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 1, 2023
@ptomulik
Copy link
Contributor Author

ptomulik commented Mar 1, 2023

possibly related or same issue: #9412

Yes, it's related.

ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 10, 2023
ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 10, 2023
ptomulik added a commit to ptomulik/psalm that referenced this issue Mar 12, 2023
@orklah orklah closed this as completed in 2327917 Mar 12, 2023
orklah added a commit that referenced this issue Mar 12, 2023
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.

3 participants