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

Is T[K] broken now? #9822

Open
zerkms opened this issue May 26, 2023 · 4 comments
Open

Is T[K] broken now? #9822

zerkms opened this issue May 26, 2023 · 4 comments
Labels

Comments

@zerkms
Copy link
Contributor

zerkms commented May 26, 2023

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

https://psalm.dev/r/be7148dbb1

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T of array
 * @template TKey of string
 * @param T $arr
 * @param TKey $k
 * @return T[TKey]
 */
function a(array $arr, string $k): mixed {
  assert(isset($arr[$k]));
  return $arr[$k];
}

$a = a(['test' => 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
Copy link
Collaborator

orklah commented May 27, 2023

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

@orklah orklah added the bug label May 27, 2023
@kkmuffme
Copy link
Contributor

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 psalm/psalm.dev#98)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/3d33f66c11
<?php

class QueryDB {}

class Compatibility {}

class DI {
   const MAPPING = array(
		'query' => QueryDB::class,
       	'comp' => Compatibility::class,
   );
    
   /**
    * @template TMapping of DI::MAPPING
    * @template TKey of key-of<DI::MAPPING>
    *
    * @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

class QueryDB {}

class Compatibility {}

class DI {
   const MAPPING = array(
		'query' => QueryDB::class,
       	'comp' => Compatibility::class,
   );
    
   /**
    * @template TMapping of DI::MAPPING
    * @template TKey of key-of<TMapping>
    *
    * @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

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