-
Notifications
You must be signed in to change notification settings - Fork 661
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
Labels
Comments
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());
|
I have a similar case. The template info is lost when checking for an instance with 'instanceof' |
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);
}
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);
}
|
This behavior is not present in psalm 4.4, shows up in 4.5 and continues in 4.6. |
This was referenced Mar 15, 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
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
The text was updated successfully, but these errors were encountered: