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

Wrong type inference #4326

Closed
marcosh opened this issue Oct 14, 2020 · 5 comments
Closed

Wrong type inference #4326

marcosh opened this issue Oct 14, 2020 · 5 comments
Labels

Comments

@marcosh
Copy link
Contributor

marcosh commented Oct 14, 2020

Consider this snippet of code (see https://psalm.dev/r/422d9413c9)

/**
 * @template S
 * @template T
 * @template A
 * @template B
 */
final class Prism
{
	/** @var callable(B): T */
    private $review;

    /** @var callable(S): Either<T, A> */
    private $preview;

    /**
     * @template C
     * @template D
     * @param Prism<A, B, C, D> $that
     */
    public function compose(Prism $that): void
    {
        /** @psalm-trace $preview */
        $preview = $that->preview;
    }
}

Psalm says that the type of $preview is callable(C): Either<D, C>, while I'm expecting it to be callable(A): Either<B, C>.

Am I doing something wrong or is Psalm inferring the wrong type here?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/422d9413c9
<?php

/**
 * @template E
 * @template F
 */
final class Either
{}

/**
 * @template S
 * @template T
 * @template A
 * @template B
 */
final class Prism
{
	/** @var callable(B): T */
    private $review;

    /** @var callable(S): Either<T, A> */
    private $preview;

    /**
     * @template C
     * @template D
     * @param Prism<A, B, C, D> $that
     */
    public function compose(Prism $that): void
    {
        /** @psalm-trace $preview */
        $preview = $that->preview;
    }
}
Psalm output (using commit 724b25b):

INFO: Trace - 32:9 - $preview: callable(C:fn-prism::compose as mixed):Either<D:fn-prism::compose as mixed, C:fn-prism::compose as mixed>

INFO: UnusedVariable - 32:9 - Variable $preview is never referenced

ERROR: MissingConstructor - 19:13 - Prism has an uninitialized property Prism::$review, but no constructor

ERROR: MissingConstructor - 22:13 - Prism has an uninitialized property Prism::$preview, but no constructor

@muglug muglug added the bug label Oct 14, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 14, 2020

Reduced to https://psalm.dev/r/565bb30e0b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/565bb30e0b
<?php

/**
 * @template A
 * @template B
 */
final class Foo
{
	/** @var Traversable<A, B> */
    public $traversable;
    
    /**
     * @param Foo<B, A> $flipped
     * @return Traversable<B, A>
     */
    public function getFlippedTraversable(Foo $flipped): Traversable
    {
        return $flipped->traversable;
    }
}
Psalm output (using commit b2382cd):

ERROR: InvalidReturnStatement - 18:16 - The inferred type 'Traversable<A:Foo as mixed, B:Foo as mixed>' does not match the declared return type 'Traversable<B:Foo as mixed, A:Foo as mixed>' for Foo::getFlippedTraversable

ERROR: InvalidReturnType - 14:16 - The declared return type 'Traversable<B:Foo as mixed, A:Foo as mixed>' for Foo::getFlippedTraversable is incorrect, got 'Traversable<A:Foo as mixed, B:Foo as mixed>'

ERROR: MissingConstructor - 10:12 - Foo has an uninitialized property Foo::$traversable, but no constructor

@muglug muglug closed this as completed in 4539a2f Oct 15, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 15, 2020

Ah, I just fixed it for methods

@muglug muglug reopened this Oct 15, 2020
@muglug muglug closed this as completed in 2902a8c Oct 15, 2020
@marcosh
Copy link
Contributor Author

marcosh commented Oct 15, 2020

that was fast, thank you!

danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
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