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

Variable detected as non-empty-list<…> fails some array shape checks that list<…> passes #4857

Open
MatmaRex opened this issue May 12, 2024 · 0 comments

Comments

@MatmaRex
Copy link

Variable detected as non-empty-list<…> fails some array shape checks that list<…> passes.

Example:

<?
/**
 * @param array<string,int|string> $a
 */
function a($a): void {
    var_dump($a);
}

/**
 * @param list<array<string,int|string>> $a
 */
function b($a): void {
    var_dump($a);
}


$x = [ 'a' => 1, 'b' => '2' ];
a($x);

$xx = [];
foreach ( [1,2,3] as $u ) {
    $xx[] = $x;
}
b($xx);

$zz = [];
foreach ( range(1,3) as $u ) {
    $zz[] = $x;
}
b($zz);

$yy = [ $x, $x, $x ];
b($yy);

Output:

input:24: PhanTypeMismatchArgument Argument 1 ($a) is $xx of type non-empty-list<array{a:1,b:'2'}> but \b() takes list<array<string,int>>|list<array<string,string>> defined at input:12

That's an incorrect error from the first foreach, but the second foreach and the literal pass.

Demo: https://phan.github.io/demo/?code=%3C%3F%0A%2F**%0A+*+%40param+array%3Cstring%2Cint|string%3E+%24a%0A+*%2F%0Afunction+a(%24a)%3A+void+{%0A++++var_dump(%24a)%3B%0A}%0A%0A%2F**%0A+*+%40param+list%3Carray%3Cstring%2Cint|string%3E%3E+%24a%0A+*%2F%0Afunction+b(%24a)%3A+void+{%0A++++var_dump(%24a)%3B%0A}%0A%0A%0A%24x+%3D+[+'a'+%3D%3E+1%2C+'b'+%3D%3E+'2'+]%3B%0Aa(%24x)%3B%0A%0A%24xx+%3D+[]%3B%0Aforeach+(+[1%2C2%2C3]+as+%24u+)+{%0A++++%24xx[]+%3D+%24x%3B%0A}%0Ab(%24xx)%3B%0A%0A%24zz+%3D+[]%3B%0Aforeach+(+range(1%2C3)+as+%24u+)+{%0A++++%24zz[]+%3D+%24x%3B%0A}%0Ab(%24zz)%3B%0A%0A%24yy+%3D+[+%24x%2C+%24x%2C+%24x+]%3B%0Ab(%24yy)%3B

Compare to PHPStan: https://phpstan.org/r/1c5904e5-ccde-47c2-ab40-6431fc5d494a

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