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

Recognize covariance when returning generic union types #5844

Closed
spawnia opened this issue Oct 27, 2021 · 2 comments · Fixed by phpstan/phpstan-src#999
Closed

Recognize covariance when returning generic union types #5844

spawnia opened this issue Oct 27, 2021 · 2 comments · Fixed by phpstan/phpstan-src#999
Labels
Milestone

Comments

@spawnia
Copy link

spawnia commented Oct 27, 2021

Bug report

See #5841

Code snippet that reproduces the problem

https://phpstan.org/r/f64c26bf-9543-4f57-9024-a415d78b2cd2

Expected output

PHPStan recognizes the narrowing of the type:

Function value() should return T of int|string but returns T of string.

However, it does not seem to recognize that T of string is covariant to T of int|string.

Separating the generics and doing an ad-hoc union seems to work: https://phpstan.org/r/673e1385-a14a-47b5-9102-a7d3d3e3ce96

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

PHPStan level 4 found a lot of redundant type checks in https://github.com/webonyx/graphql-php. A series of pull requests driven by the analysis lead to a 10-20% performance boost.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Jan 31, 2022
@ondrejmirtes
Copy link
Member

/cc @arnaud-lb This bug also looks like something up your alley :)

@github-actions
Copy link

github-actions bot commented Mar 9, 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 Mar 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants