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

incorrect InvalidArgument when using generic arrays of multiple value types #6563

Closed
azjezz opened this issue Oct 1, 2021 · 11 comments
Closed

Comments

@azjezz
Copy link
Contributor

azjezz commented Oct 1, 2021

see: https://psalm.dev/r/e80523194f

expected: no errors, the array passed is of type array<string, Node<string>|Node<int>>

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T
 */
final class Node {
  /**
   * @param T $value
   */
  public function __construct(public mixed $value) {}
}

/**
 * @template Tk of array-key
 * @template Tv
 *
 * @param array<Tk, Node<Tv>> $_array
 */
function a(array $_array): void {}


a(['a' => new Node('a')]);

a(['a' => new Node('a'), 'b' => new Node(3)]);
Psalm output (using commit 97a3d67):

ERROR: InvalidArgument - 24:1 - Incompatible types found for Tv

@azjezz
Copy link
Contributor Author

azjezz commented Oct 1, 2021

actually, not a bug, but the error message should be clearer ( Node is not covariant ).

https://psalm.dev/r/991ca76408

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/991ca76408
<?php

/**
 * @template-covariant T
 */
final class Node {
  /**
   * @param T $value
   */
  public function __construct(public mixed $value) {}
}

/**
 * @template Tk of array-key
 * @template Tv
 *
 * @param array<Tk, Node<Tv>> $_array
 */
function a(array $_array): void {}


a(['a' => new Node('a')]);

a(['a' => new Node('a'), 'b' => new Node(3)]);
Psalm output (using commit 97a3d67):

No issues!

@orklah
Copy link
Collaborator

orklah commented Jan 30, 2022

I'll close this, the message was improved slightly

@orklah orklah closed this as completed Jan 30, 2022
@AndrolGenhald
Copy link
Collaborator

Seems like this could still be improved to work like this without requiring the @var annotation.

@psalm-github-bot

This comment has been minimized.

@orklah
Copy link
Collaborator

orklah commented Jan 30, 2022

@AndrolGenhald The real fix is: https://psalm.dev/r/45db240ac3

The message was cryptic before, now it explains the issue, but it doesn't show the possible solution

@psalm-github-bot

This comment has been minimized.

@AndrolGenhald
Copy link
Collaborator

I think it's also fine to widen the type even when the template isn't covariant, but I guess I'm fine requiring that to be an explicit annotation.

I know we've talked about some sort of alternate annotation to @var that's not so dangerous, it would be nice to have that here too for type widening (eg /** @strict-var int */ $i = 1 widens $i from 1 to int, but /** @strict-var int */ $i = 'foo'; should emit an issue).

@weirdan
Copy link
Collaborator

weirdan commented Jan 30, 2022

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2c5a043897
<?php

/**
 * @template T
 */
final class Node {
  /**
   * @param T $value
   */
  public function __construct(public mixed $value) {}
}

/**
 * @template Tk of array-key
 * @template Tv
 *
 * @param array<Tk, Node<Tv>> $_array
 */
function a(array $_array): void {}


a(['a' => new Node('a')]);

/** @var list<Node<'a'|3>> */
$nodes = ['a' => new Node('a'), 'b' => new Node(3)];
a($nodes);
Psalm output (using commit 31941d1):

No issues!
https://psalm.dev/r/45db240ac3
<?php

/**
 * @template-covariant T
 */
final class Node {
  /**
   * @param T $value
   */
  public function __construct(public mixed $value) {}
}

/**
 * @template Tk of array-key
 * @template Tv
 *
 * @param array<Tk, Node<Tv>> $_array
 */
function a(array $_array): void {}


a(['a' => new Node('a')]);

$nodes = ['a' => new Node('a'), 'b' => new Node(3)];
a($nodes);
Psalm output (using commit 31941d1):

No issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants