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

array_merge of known shapes loses type information #5846

Closed
Firehed opened this issue Oct 27, 2021 · 7 comments · Fixed by phpstan/phpstan-src#1155
Closed

array_merge of known shapes loses type information #5846

Firehed opened this issue Oct 27, 2021 · 7 comments · Fixed by phpstan/phpstan-src#1155

Comments

@Firehed
Copy link
Contributor

Firehed commented Oct 27, 2021

Bug report

Performing an array_merge where two arrays share a known shape results in type loss of the result.

Code snippet that reproduces the problem

Original issue:

https://phpstan.org/r/bc48b3cb-e1f4-4b4a-81ca-d18bf8901104

Simplest form:

https://phpstan.org/r/34b0de0f-a3b6-4ed5-b802-ff500ebbb619

Expected output

No errors, dumpType shows:

Dumped type: array{a: int, b: string}
@staabm
Copy link
Contributor

staabm commented Oct 28, 2021

Its a know problem. I guess a issue is already filled somewhere.

Last time we tried to inprove this case we had to revert because the analysis was that time-intensive that it was not acceptable - phpstan/phpstan-src#622 (comment)

@voku
Copy link
Contributor

voku commented Oct 28, 2021

There is already a new pull request that (at least in my tests) do not have the performance issue and still fix the array_merge logic: phpstan/phpstan-src#696

@staabm
Copy link
Contributor

staabm commented Mar 30, 2022

fixed by phpstan/phpstan-src#1135

@phpstan-bot
Copy link
Contributor

@Firehed After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-15: Dumped type: non-empty-array<literal-string&non-empty-string, int|string>
-16: Parameter #1 $s of function useString expects string, int|string given.
-17: Parameter #1 $i of function useInt expects int, int|string given.
+15: Dumped type: array{a: int, b: string}
Full report
Line Error
15 Dumped type: array{a: int, b: string}

@phpstan-bot
Copy link
Contributor

@Firehed After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
  7: Dumped type: array{a: 1, b: 'bee'}
- 8: Dumped type: non-empty-array<literal-string&non-empty-string, 1|'bee'>
- 9: Parameter #1 $s of function useString expects string, int|string given.
-10: Parameter #1 $i of function useInt expects int, int|string given.
+ 8: Dumped type: array{a: 1, b: 'bee'}
Full report
Line Error
7 Dumped type: array{a: 1, b: 'bee'}
8 Dumped type: array{a: 1, b: 'bee'}

@ondrejmirtes
Copy link
Member

Needs a regression test :) /cc @VincentLanglet

@github-actions
Copy link

github-actions bot commented May 3, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants