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

Covariance problem with template type with $this and static #9195

Closed
mvorisek opened this issue Apr 15, 2023 · 6 comments
Closed

Covariance problem with template type with $this and static #9195

mvorisek opened this issue Apr 15, 2023 · 6 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Apr 15, 2023

Bug report

In #9153 (comment) I was advised to use @template instead of @template-covariant. But then I get another phpstan error:

Method MethodMock::methodName() should return MagicMethod<static(MethodMock)> but returns
MagicMethod<$this(MethodMock)>.
Template type TTargetClass on class MagicMethod is not covariant. Learn more:
[What's Up With @template-covariant?](https://phpstan.org/blog/whats-up-with-template-covariant)

I would say the phpstan error (with @template) is wrong, as $this is strictly a subtype of static.

Code snippet that reproduces the problem

https://phpstan.org/r/6897d3da-b2b5-4a31-b740-927310bd6e72 (with @template-covariant)

https://phpstan.org/r/75af0bc1-1122-4734-8b36-b8cc966c32df (with @template)

Expected output

no error

@ondrejmirtes
Copy link
Member

as $this is strictly a subtype of static.

And that's exactly why this error is reported. The linked article explains it.

@mvorisek
Copy link
Contributor Author

mvorisek commented Apr 15, 2023

@ondrejmirtes Yes, @template-covariant should be probably used, but then I get the Template type TTargetClass is declared as covariant, but occurs in invariant position in property MagicAbstract::$_atk__core__hintable_magic__class. (first repro link).

@ondrejmirtes
Copy link
Member

Yes. You can't have everything because that leads to unsafe code.

@mvorisek
Copy link
Contributor Author

mvorisek commented Apr 15, 2023

@ondrejmirtes thank you. one phpstan ignore /w @template-covariant fixed the phpstan errors.

Still have one question - why Method MethodMock::methodName() should return MagicMethod<static(MethodMock)> but returns MagicMethod<$this(MethodMock)>. is a problem with invariant template? static vs $this is no different in sense of classes, the only difference is the returned type is asserted for specific object instance, which is strictly stronger/safer assumption.

@ondrejmirtes
Copy link
Member

@mvorisek $this(MethodMock) is a subtype of static(MethodMock). Meaning not every static(MethodMock) is also $this(MethodMock). And there's no special-case in the code to allow this even without @template-covariant.

It might start working when this suggestion is implemented #6732 (comment)

@mvorisek mvorisek changed the title Covariance problem with template type with $this and static Covariance problem with template type with $this and static in trait Apr 15, 2023
@mvorisek mvorisek changed the title Covariance problem with template type with $this and static in trait Covariance problem with template type with $this and static Apr 15, 2023
@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 May 17, 2023
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