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

Generics and static type #3118

Closed
amorimjuliana opened this issue Mar 24, 2020 · 15 comments
Closed

Generics and static type #3118

amorimjuliana opened this issue Mar 24, 2020 · 15 comments

Comments

@amorimjuliana
Copy link

Bug report

PHPStan complains about returning a subclass of a generic class that is compatible with the return type.

Code snippet that reproduces the problem

https://phpstan.org/r/df0e168c-f207-4db8-9d24-e1324e287652

Expected output

No errors.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Mar 24, 2020
@muglug
Copy link
Contributor

muglug commented Mar 24, 2020

Note this is only a false-positive if the class that makes Enum<static> into an explicit collection is declared final (as in the example given).

Otherwise this pattern can lead to runtime errors: https://3v4l.org/E78KQ

@amorimjuliana
Copy link
Author

Yes, and that should be de case for all enums and enum sets :)

@muglug
Copy link
Contributor

muglug commented Mar 24, 2020

Yeah, just making a note for whoever tries to fix this :)

@Majkl578
Copy link
Contributor

Majkl578 commented Aug 5, 2020

Another, simplified reproducer: https://phpstan.org/r/0a7c0617-a38b-42f2-b1b1-d059e0a80715

@ComiR
Copy link

ComiR commented Aug 24, 2020

I think this is the same problem, or at least related to the problem, I am having:
https://phpstan.org/r/72320485-b6ac-477d-8cac-6ffacd813de6

@ondrejmirtes
Copy link
Member

@ComiR You're breaking the interface contract. Your code wuold only be valid if Hello was a final class (but PHPStan currently complains which is a bug but a different one).

If you create class Foo extends Hello but still return Hello from Foo::create() (which is what the method will do if not overriden) then World::createGreeter(Foo::class); will return Hello but not Foo which breaks the contract.

@ComiR
Copy link

ComiR commented Aug 24, 2020

Thanks @ondrejmirtes, I forgot to make the class Hello final in this example. The error stays the same: https://phpstan.org/r/19d1dd4a-1535-4ca5-9cd9-779743b92d5c

@ondrejmirtes
Copy link
Member

Yeah, that's a bug, but you can work around it: https://phpstan.org/r/7e2056c0-7b2e-4cfe-bf95-3d445e542406

@phpstan-bot
Copy link
Contributor

@amorimjuliana PHPStan now reports different result with your code snippet:

@@ @@
-42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+PHP 7.4 – 8.0 (1 error)
+==========
+
+42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+
+PHP 7.1 – 7.3 (1 error)
+==========
+
+42: Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all().
Full report

PHP 7.4 – 8.0 (1 error)

Line Error
42 Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()

PHP 7.1 – 7.3 (1 error)

Line Error
42 Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all().

@phpstan-bot
Copy link
Contributor

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

@@ @@
-13: Method Test::foo() should return static(Test) but returns Test.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-No errors
+19: Return type (Hello) of method Hello::create() should be covariant with return type (static(Greeter)) of method Greeter::create()
Full report
Line Error
19 Return type (Hello) of method Hello::create() should be covariant with return type (static(Greeter)) of method Greeter::create()

@phpstan-bot
Copy link
Contributor

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

@@ @@
-19: Return type (Hello) of method Hello::create() should be covariant with return type (static(Greeter)) of method Greeter::create()
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+PHP 7.4 – 8.0
+==========
+
+No errors
+
+PHP 7.1 – 7.3 (1 error)
+==========
+
+42: Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all().
Full report

PHP 7.4 – 8.0

No errors

PHP 7.1 – 7.3 (1 error)

Line Error
42 Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all().

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src@d225a68

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Mar 17, 2021
@github-actions
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 Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants