-
-
Notifications
You must be signed in to change notification settings - Fork 866
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
False positive with template covariant #9161
Comments
Summoning @jiripudil to judge it here 😆 |
Hi, referencing phpstan/phpstan-src#2054 for context. I've stumbled upon a very similar error in phpstan's integration tests there. The issue is that the array shape / tuple type must be invariant because there's nothing that prevents you from writing into it: /**
* @param Map<int, Fruit>
*/
function doMagicWithFruit(Map $fruits)
{
[$firstPair] = $fruits->toPairs();
$firstPair[1] = new Apple();
} The covariant template type makes it possible to pass a Plain arrays (and, transitively, lists) do not behave so strictly type-wise (#8880 (comment)), and therefore do not need to be so strict variance-wise. I have my reservations about that (see my previous comment in the linked issue), but that's a different (and more complicated) discussion. |
Writing into arrays does not matter if they're not passed by reference, right? And references are they own beasts which are generally hands-off by the static analyser because they bring a lot of mess with themselves. |
Oh, yeah, right! But in that case, I'd say this is indeed a bug. |
I'd be really grateful if you could fix it and test it 😊 Thank you. |
Fixed: phpstan/phpstan-src#2464 |
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. |
Bug report
I'm not 100% sure but I don't think there is much of a difference between toArray and ToPairs regarding type variance, right?
Or In case the error is correct then how should I fix it? 🤔
Code snippet that reproduces the problem
https://phpstan.org/r/ccc12749-6dfd-4e0d-abf5-54dcd11e6fd6
Expected output
no error
The text was updated successfully, but these errors were encountered: