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

array_splice with two lists should produce a list #2942

Closed
dbrekelmans opened this issue Mar 8, 2020 · 2 comments
Closed

array_splice with two lists should produce a list #2942

dbrekelmans opened this issue Mar 8, 2020 · 2 comments
Labels

Comments

@dbrekelmans
Copy link

I would not expect to see this error since list<E> and array<int, E> are practically the same.

https://psalm.dev/r/25b06d9cfd

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/25b06d9cfd
<?php

/**
 * @template E as object
 */
final class Foo {
    /** 
     * @psalm-var list<E>
     * @var array<int, object>
     **/
    private $elements;
    
    /**
     * @psalm-param array<array-key, E> $elements
     * @param array<int|string, object> $elements
     */
    public function __construct(array $elements = [])
    {
        $this->elements = array_values($elements);
    }
    
    /**
     * @psalm-param E $element
     */
    public function bar(int $index, object $element) : void
    {
        array_splice($this->elements, $index, 0, [$element]);
    }
}
Psalm output (using commit 8926f1d):

ERROR: PropertyTypeCoercion - 27:22 - $this->elements expects 'list<E:Foo as object>',  parent type 'array<int, E:Foo as object>' provided

@weirdan
Copy link
Collaborator

weirdan commented Mar 8, 2020

It shouldn't complain here, not for the reason you state (list and array<int,_> are different), but because array_splice on lists outputs lists.

@weirdan weirdan added the bug label Mar 8, 2020
@muglug muglug changed the title expects list<E>, parent type array<int, E> provided array_splice with two lists should produce a list Mar 9, 2020
@muglug muglug closed this as completed in 5b4ed6b Mar 9, 2020
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

2 participants