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

Getting errors about inferred and declared return types, but they don't make sense. #8672

Open
ollieread opened this issue Nov 5, 2022 · 6 comments

Comments

@ollieread
Copy link

ollieread commented Nov 5, 2022

I have the following method.

/**
 * @param E                               $element
 * @param \Smpl\Utils\Contracts\Predicate $filter
 * @return list<E>
 */
private function getElementsThatAre(mixed $element, Predicate $filter): array
{
    $elements = [];

    foreach ($this->elements as $existingElement) {
        $result = get_sign($this->getComparator()->compare($existingElement, $element));

        if ($filter($result)) {
            $elements[] = $existingElement;
        }
    }

    /** @infection-ignore-all */
    if (! empty($elements)) {
        usort($elements, $this->getComparator());
    }

    return $elements;
}

Which is causing the following error.

ERROR: MixedReturnTypeCoercion - src/Concerns/SortsCollection.php:284:16 - The declared return type 'list<int>' for Smpl\Collections\Concerns\SortsCollection::getElementsThatAre is more specific than the inferred return type 'list<(E:Smpl\Collections\Concerns\SortsCollection as mixed)|(E:Smpl\Collections\SortedQueue as mixed)>' (see https://psalm.dev/197)
     * @return list<E>

This method is not defined as returning list<int>, which appears nowhere in my codebase

image

@ollieread
Copy link
Author

Hey @ollieread, can you reproduce the issue on https://psalm.dev ?

No, cannot recreate it.

@ollieread
Copy link
Author

The trait that method is in, has the following class docblock.

/**
 * @template-covariant  I of mixed
 * @template            E of mixed
 *
 * @requires \Smpl\Collections\Contracts\SortedCollection<I, E>
 *
 * @mixin \Smpl\Collections\Contracts\Collection<I, E>
 */
trait SortsCollection {}

Everywhere that the trait is used, there's also this.

/** @use \Smpl\Collections\Concerns\SortsCollection<int, E> */
use SortsCollection;

It looks like it may, for some reason, be using I instead of E.

@ollieread
Copy link
Author

I'm also having the following error with 5 other methods in this trait.

ERROR: MixedReturnTypeCoercion - src/Concerns/SortsCollection.php:178:16 - The declared return type 'int|null' for Smpl\Collections\Concerns\SortsCollection::lower is more specific than the inferred return type '(E:Smpl\Collections\SortedQueue as mixed)|null' (see https://psalm.dev/197)
     * @return E|null

@orklah
Copy link
Collaborator

orklah commented Nov 6, 2022

Could you post the content of Collection and SortsCollection also? Or even better if you have the codebase available somewhere

@ollieread
Copy link
Author

Could you post the content of Collection and SortsCollection also? Or even better if you have the codebase available somewhere

I've pushed all of my local changes (There were a lot) to a temporary branch called temporary-debug here: https://github.com/smplphp/collections/tree/temporary-debug

Here's SortsCollection https://github.com/smplphp/collections/blob/temporary-debug/src/Concerns/SortsCollection.php

And here's Collection https://github.com/smplphp/collections/blob/temporary-debug/src/Contracts/Collection.php

Let me know if you have any more specific questions, I'm aware that as far as small packages go, it isn't XD

@ollieread
Copy link
Author

ollieread commented Nov 7, 2022

As a follow-up, the directory src/Concerns is ignored on that branch @orklah.

With the <directory> entry in psalm.xml, you get:

image

But without it, you get:

image

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