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

Inconsistent behavior with template-extends and template constraints #5229

Closed
simitter opened this issue Feb 15, 2021 · 4 comments
Closed

Inconsistent behavior with template-extends and template constraints #5229

simitter opened this issue Feb 15, 2021 · 4 comments
Labels

Comments

@simitter
Copy link

Hello,

If a class extends a template and puts in the constraints directly in the extends tag seems to work fine.
If template extends derives the constraints from another template psalm reports a MixedArgumentTypeCoercion

It seemed to work in the 4.4 versions.
Is this a bug or am i missing something?

https://psalm.dev/r/6258a74579

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6258a74579
<?php

/**
 * @psalm-template T
 */
abstract class Base {}

/**
 * @psalm-template T2 of int|float
 *
 * @template-extends Base<T2>
 */
final class NotWorking extends Base {}

/**
 * @template-extends Base<int|float>
 */
final class Working extends Base {}

/** @psalm-suppress UnusedParam */
function testNotWorking(NotWorking $notWorking): void {}

/** @psalm-suppress UnusedParam */
function testWorking(Working $working): void {}

testNotWorking(new NotWorking());
testWorking(new Working());
Psalm output (using commit 86ba3b0):

INFO: MixedArgumentTypeCoercion - 26:16 - Argument 1 of testNotWorking expects NotWorking<float|int>, parent type NotWorking provided

@githoober
Copy link

githoober commented Feb 19, 2021

I have a similar case. The template info is lost when checking for an instance with 'instanceof'

https://psalm.dev/r/a73291657c

https://psalm.dev/r/9247b4d5d6

@psalm-github-bot
Copy link

psalm-github-bot bot commented Feb 19, 2021

I found these snippets:

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

class Element {}

/**
* @template E of Element
*/
class ElementList {}

/**
* @param Element|ElementList<Element>|mixed $data
*/
function test($data): void
{
    if ($data instanceof Element) {
    } else if ($data instanceof ElementList) {
        acceptList($data);
    }        
}

/**
* @param ElementList<Element> $list
*/
function acceptList(ElementList $list): void
{
    echo get_class($list);
}
Psalm output (using commit e93e532):

INFO: MixedArgumentTypeCoercion - 17:20 - Argument 1 of acceptList expects ElementList<Element>, parent type ElementList provided
https://psalm.dev/r/9247b4d5d6
<?php

class Element {}

class ChildElement extends Element {}

/**
* @template E of Element
*/
class ElementList {}

/**
* @param ChildElement|ElementList<ChildElement>|mixed $data
*/
function test($data): void
{
    if ($data instanceof ChildElement) {
        
    } else if ($data instanceof ElementList) {
        acceptList($data);
    }        
}

/**
* @param ElementList<ChildElement> $list
*/
function acceptList(ElementList $list): void
{
    echo get_class($list);
}
Psalm output (using commit e93e532):

INFO: MixedArgumentTypeCoercion - 20:20 - Argument 1 of acceptList expects ElementList<ChildElement>, parent type ElementList provided

@githoober
Copy link

githoober commented Feb 19, 2021

This behavior is not present in psalm 4.4, shows up in 4.5 and continues in 4.6.

@weirdan weirdan added the bug label Feb 25, 2021
@muglug muglug closed this as completed in 474ebf9 Feb 26, 2021
This was referenced Mar 15, 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

3 participants