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 generic type lost after conditionally adding more data into array #10445

Closed
Firehed opened this issue Jan 16, 2024 · 2 comments
Closed

Comments

@Firehed
Copy link
Contributor

Firehed commented Jan 16, 2024

Bug report

When an array contains a generic value at a known index, and then another value is conditionally added (e.g. only if something is non-empty), the type of the first value gets lost. See snippet + expected output for details.

Note that I have the attached template declared as @template-covariant but the issue doesn't seem specific to that.

Code snippet that reproduces the problem

https://phpstan.org/r/f508e2b7-f6f9-4919-8502-cd1a1cdd105a

Expected output

No errors (other than the dumpType() results added to step through the error).

The dumped type on lines 36 and 39 are both correct; observe that the type at the result index changes on line 41, which is incorrect and causes the complaint on the return value.

Also, I've observed that restructuring this to add result after errors does not exhibit this issue. Similarly, not adding errors conditionally behaves as expected from a typechecker perspective (though not what I need at runtime).

Did PHPStan help you today? Did it make you happy in any way?

PHPStan makes me develop faster and more accurately every day!

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@8342785

Copy link

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 Feb 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants