vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 662 forks source link

False-positive `InvalidTemplateParam` with `@template-covariant T` when a callable parameter accepts `T` #10848

Open Shira-3749 opened 6 months ago

Shira-3749 commented 6 months ago

When a class defines @template-covariant T and one of the methods has a callable parameter which accepts T as an argument, I'm getting an InvalidTemplateParam error:

https://psalm.dev/r/2eb66f01fe

I'm not sure the error is valid in this case. (And it seems to work fine otherwise.)

PHPStan doesn't seem to mind this: https://phpstan.org/r/ac9be5aa-4437-40c3-b55d-3ffaa236cb67 But will also error when it's an actual issue: https://phpstan.org/r/96d33c99-1ff7-4541-b024-c8cfaea34e30

I've also tried it in C# (as a language with native generics support) and it seems to allow this as well:

public interface IExample<out T>
{
    public void call(Action<T> callback);
}

(out is equivalent to @template-covariant here)

psalm-github-bot[bot] commented 6 months ago

I found these snippets:

https://psalm.dev/r/2eb66f01fe ```php value); } } ``` ``` Psalm output (using commit ef3b018): ERROR: InvalidTemplateParam - 15:15 - Template param T of Example is marked covariant and cannot be used here ```
https://psalm.dev/r/3ebad94817 ```php value); } } class Animal {} class Cat extends Animal {} class Dog extends Animal {} /** @psalm-suppress UnusedClosureParam */ function exampleWithObjects(): void { $example = new Example(new Cat()); $example->call(function (Animal $animal) {}); $example->call(function (Cat $animal) {}); $example->call(function (Dog $animal) {}); // OK (expected error) } /** * @param Cat[] $cats * @param callable(Animal[]):void $takesAnimals * @param callable(Cat[]):void $takesCats * @param callable(Dog[]):void $takesDogs */ function exampleWithArrays( array $cats, callable $takesAnimals, callable $takesCats, callable $takesDogs, ): void { $example = new Example($cats); $example->call($takesAnimals); $example->call($takesCats); $example->call($takesDogs); // OK (expected error) } /** * @param \ArrayObject $cats * @param callable(\ArrayObject):void $takesAnimals * @param callable(\ArrayObject):void $takesCats * @param callable(\ArrayObject):void $takesDogs */ function exampleWithArrayObjects( \ArrayObject $cats, callable $takesAnimals, callable $takesCats, callable $takesDogs, ): void { $example = new Example($cats); $example->call($takesAnimals); // OK (expected error) $example->call($takesCats); $example->call($takesDogs); // OK (expected error) } ``` ``` Psalm output (using commit ef3b018): ERROR: InvalidArgument - 35:20 - Argument 1 of Example::call expects callable(Cat):void, but pure-Closure(Dog):void provided ERROR: InvalidArgument - 54:20 - Argument 1 of Example::call expects callable(array):void, but callable(array):void provided ERROR: InvalidArgument - 72:20 - Argument 1 of Example::call expects callable(ArrayObject):void, but callable(ArrayObject):void provided ERROR: InvalidArgument - 74:20 - Argument 1 of Example::call expects callable(ArrayObject):void, but callable(ArrayObject):void provided ```
weirdan commented 6 months ago

Note to future me: callable(T):void should be valid in this context, but callable():T shouldn't.

vkurdin commented 4 months ago

It would be awesome if Psalm supported this Scala machinery on callables and variance: https://scastie.scala-lang.org/ZbzouFbvRHGPSwwB8OrNoA +A stands for @template-covariant A -A is @template-contravariant A Unit is void A => Boolean is callable(A): bool (A => Boolean) => Boolean is callable(callable(A): bool): bool