vimeo / psalm

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

Is T[K] broken now? #9822

Open zerkms opened 1 year ago

zerkms commented 1 year ago

Example from the documentation: https://psalm.dev/docs/annotating_code/type_syntax/utility_types/#tk

https://psalm.dev/r/be7148dbb1

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/be7148dbb1 ```php 123], 'test'); /** @psalm-trace $a */; // 123 ``` ``` Psalm output (using commit b99857c): INFO: MixedReturnStatement - 12:10 - Could not infer a return type INFO: MixedInferredReturnType - 8:12 - Could not verify return type 'T[TKey]' for a INFO: Trace - 16:23 - $a: 123 INFO: UnusedVariable - 15:1 - $a is never referenced or the value is not used ```
orklah commented 1 year ago

Well, it seems to work but Mixed* issues happens. We should try to get rid of those

kkmuffme commented 1 year ago

It doesn't work here too? https://psalm.dev/r/3d33f66c11

(like https://psalm.dev/r/e4eb7a3e05 doesn't work either but there seems to be a separate bug https://github.com/psalm/psalm.dev/issues/98)

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/3d33f66c11 ```php QueryDB::class, 'comp' => Compatibility::class, ); /** * @template TMapping of DI::MAPPING * @template TKey of key-of * * @param TKey $arg * @return TMapping[TKey] */ public function get( $arg ) { $v = self::MAPPING[ $arg ]; /** @psalm-trace $v */; return $v; } } $d = new DI(); $r = $d->get( 'comp' ); $c = new $r(); /** @psalm-trace $c */; ``` ``` Psalm output (using commit 106b986): INFO: MixedAssignment - 28:1 - Unable to determine the type that $r is being assigned to INFO: MixedMethodCall - 29:6 - Cannot call constructor on an unknown class INFO: Trace - 30:23 - $c: object INFO: UnusedVariable - 29:1 - $c is never referenced or the value is not used INFO: Trace - 22:30 - $v: Compatibility::class|QueryDB::class ERROR: InvalidReturnStatement - 23:15 - The inferred type 'Compatibility::class|QueryDB::class' does not match the declared return type 'TMapping[TKey]' for DI::get ERROR: InvalidReturnType - 18:15 - The declared return type 'TMapping[TKey]' for DI::get is incorrect, got 'Compatibility::class|QueryDB::class' ```
https://psalm.dev/r/e4eb7a3e05 ```php QueryDB::class, 'comp' => Compatibility::class, ); /** * @template TMapping of DI::MAPPING * @template TKey of key-of * * @param TKey $arg * @return TMapping[TKey] */ public function get( $arg ) { $v = self::MAPPING[ $arg ]; /** @psalm-trace $v */; return $v; } } $d = new DI(); $r = $d->get( 'comp' ); $c = new $r(); /** @psalm-trace $c */; ``` ``` Psalm encountered an internal error: /vendor/vimeo/psalm/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php: count(): Argument #1 ($value) must be of type Countable|array, null given ```