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

Strange behavior when assigning to list and class-string-map #9412

Closed
ptomulik opened this issue Feb 26, 2023 · 3 comments
Closed

Strange behavior when assigning to list and class-string-map #9412

ptomulik opened this issue Feb 26, 2023 · 3 comments

Comments

@ptomulik
Copy link
Contributor

False negative for list and false positive for class-string-map. I think these two problems are related:

https://psalm.dev/r/1abd6b7b03

I guess, also #4460 is related.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1abd6b7b03
<?php

final class ListTest
{
    /** @var list */
    private array $list = [];
    
    public function set1(int $i, mixed $v): void {
        $this->list[$i] = $v; // OK, it's an error
    }
    
    public function set2(int $i, mixed $v): void {
        if (isset($this->list[$i])){} // no-op
        $this->list[$i] = $v; // WRONG, it should still be an error
    }
}

final class ClassMapTest
{
    /** @var class-string-map<T,T> */
    private array $map = [];
    
    /**
     * @template U of object
     * @param class-string<U> $k
     * @param U $v
     */
    public function set1(string $k, object $v): void {
        $this->map[$k] = $v; // false positive
    }
    
    /**
     * @template U of object
     * @param class-string<U> $k
     * @param U $v
     */
    public function set2(string $k, object $v): void {
        if (isset($this->map[$k])){} // no-op
        $this->map[$k] = $v; // somehow the false positive is silenced
    }
}
Psalm output (using commit 8f98539):

ERROR: PropertyTypeCoercion - 9:9 - $this->list expects 'list<mixed>',  parent type 'non-empty-array<int, mixed>' provided

ERROR: InvalidPropertyAssignmentValue - 29:9 - $this->map with declared type 'class-string-map<T as object, T:class-string-map as object>' cannot be assigned type 'non-empty-array<(class-string<T:class-string-map as object>)|(class-string<U:fn-classmaptest::set1 as object>), (T:class-string-map as object)|(U:fn-classmaptest::set1 as object)>'

@ptomulik
Copy link
Contributor Author

ptomulik commented Mar 1, 2023

The false-positive ERROR: InvalidPropertyAssignmentValue - 29:9 gone (fixed in #9415). The bug that alters ListTest has still to be fixed.

@orklah
Copy link
Collaborator

orklah commented Mar 31, 2023

Seems fixed :)

@orklah orklah closed this as completed Mar 31, 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

No branches or pull requests

2 participants