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

Templated types defined as union types causing unexpected InvalidArgument errors #8280

Closed
jaikdean opened this issue Jul 18, 2022 · 5 comments

Comments

@jaikdean
Copy link

This issue seems to have been introduced in 4.24.0.

https://psalm.dev/r/dbb9cbe3c0

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T of int|numeric-string
 */
class Foo {}

function bar(Foo $foo): void {
    echo $foo::class;
}

/** @var Foo<numeric-string> $foo */
$foo = new Foo();

bar($foo);
Psalm output (using commit 8c716f8):

ERROR: InvalidArgument - 15:5 - Argument 1 of bar expects Foo<int|numeric-string>, Foo<numeric-string> provided

@jaikdean
Copy link
Author

I suspect it was introduced by #8069

@AndrolGenhald
Copy link
Collaborator

This is actually correct, and was previously a false-negative. If you don't mark the template as covariant you can run into this type of bug (which is a huge problem if, for example, $foo is a reference to another object's property marked as Foo<numeric-string> which suddenly becomes Foo<int>).

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8ea443e090
<?php

/**
 * @template-covariant T of int|numeric-string
 */
class Foo {}

function bar(Foo $foo): void {
    echo $foo::class;
}

/** @var Foo<numeric-string> $foo */
$foo = new Foo();

bar($foo);
Psalm output (using commit 8c716f8):

No issues!

@jaikdean
Copy link
Author

Very interesting, thanks @AndrolGenhald.

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