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

Edit an array, member of a Weakmap is wrongly thought to be assigned to the Weakmap member itself #11126

Open
mathroc opened this issue Oct 15, 2024 · 1 comment

Comments

@mathroc
Copy link
Contributor

mathroc commented Oct 15, 2024

I don't know how to explain it better than the issue title 😅

https://psalm.dev/r/79578e4ab5

not sure it's only related to Weakmap, but it works when using a plain array

Copy link

I found these snippets:

https://psalm.dev/r/79578e4ab5
<?php

/**
 * @param array<string,array<string,int>> $wm
 */
function g(array $wm, string $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $wm[$k][$s] ??= 0;
  $wm[$k][$s]++;
}

/**
 * @param \WeakMap<object,array<string,int>> $wm
 */
function f1(\Weakmap $wm, object $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $wm[$k][$s] ??= 0;
  $wm[$k][$s]++;
}

/**
 * @param \WeakMap<object,array<string,int>> $wm
 */
function f2(\Weakmap $wm, object $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $a = $wm[$k];
  $a[$s] ??= 0;
  $a[$s]++;
}
Psalm output (using commit 03ee02c):

INFO: MixedAssignment - 17:3 - Unable to determine the type of this assignment

INFO: MixedArgument - 17:3 - Argument 2 of WeakMap::offsetSet cannot be 0|mixed, expecting array<string, int>

ERROR: PossiblyInvalidArgument - 17:3 - Argument 2 of WeakMap::offsetSet expects array<string, int>, but possibly different type 0|mixed provided

INFO: MixedOperand - 18:3 - Left operand cannot be mixed

INFO: MixedAssignment - 18:3 - Unable to determine the type of this assignment

INFO: MixedArgument - 18:3 - Argument 2 of WeakMap::offsetSet cannot be mixed, expecting array<string, int>

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

1 participant