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 inheritance fetching return type #4537

Closed
thomasvargiu opened this issue Nov 12, 2020 · 3 comments
Closed

Wrong inheritance fetching return type #4537

thomasvargiu opened this issue Nov 12, 2020 · 3 comments

Comments

@thomasvargiu
Copy link
Contributor

Same as #4524 but for return types, it seems that return types as not fetched from the right parent class/interface.

I don't know if this is the right example, but debugging I noticed this bug.

https://psalm.dev/r/7f937bb1d0

Since Foo::test() returns Foo, which implements B, which implements A, the signature should be valid.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7f937bb1d0
<?php

/**
 * @template T
 */
interface A
{
    /**
     * @template F
     * @param F $value
     * @return A<F>
     */
    public function map($value): A;
}

/**
 * @template T
 * @template-extends A<T>
 */
interface B extends A
{
    /**
     * @template F
     * @param F $value
     * @return B<F>
     */
    public function map($value): A;
}

/**
 * @template T
 * @template-implements B<T>
 */
class Foo implements B
{
    /**
     * @var T
     */
    private $value;

    /**
     * @param T $value
     */
    public function __construct($value)
    {
        $this->value = $value;
    }

    
    public function map($value): A
    {
        return new Foo($value);
    }
}

$foo = new Foo('str');
/** @psalm-trace $value */
$value = $foo->map('string');
Psalm output (using commit b7551e7):

INFO: Trace - 58:1 - $value: A<empty>

@muglug
Copy link
Collaborator

muglug commented Nov 12, 2020

Simplified without generics: https://psalm.dev/r/d3c5472f7a

@psalm-github-bot
Copy link

I found these snippets:

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

interface A
{
    /**
     * @return A
     */
    public function map(): A;
}

interface B extends A
{
    /**
     * @return B
     */
    public function map(): A;
}

class F implements B
{
    public function map(): A {
        return new F();
    }
}

function takesB(B $f) : B {
    return $f->map();
}

function takesF(F $f) : B {
    return $f->map();
}
Psalm output (using commit 929efcc):

INFO: LessSpecificReturnStatement - 31:12 - The type 'A' is more general than the declared return type 'B' for takesF

INFO: MoreSpecificReturnType - 30:25 - The declared return type 'B' for takesF is more specific than the inferred return type 'A'

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