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

Integer ranges in array keys do not carry through generics #7087

Closed
lcdennison opened this issue Apr 22, 2022 · 2 comments
Closed

Integer ranges in array keys do not carry through generics #7087

lcdennison opened this issue Apr 22, 2022 · 2 comments
Labels
Milestone

Comments

@lcdennison
Copy link

Bug report

Using 1.5.7.

In the below example link, I created two methods (foo and bar) that accept an array parameter. foo declares an explicit return of array<int<0,max>, mixed>, whereas bar uses generics to simply pass through the input type. The class has two properties, array1 and array2 that both expect array<int<0,max>, mixed>. The result of foo can be successfully assigned to a property, but the result of bar cannot. It would seem the generics I used for bar do not carry through the integer range int<0,max> for keys. It comes out as simply int.

Or I'm using generics incorrectly?

Code snippet that reproduces the problem

https://phpstan.org/r/e9901d5a-abb7-47ba-8706-204c3890a6fb

Expected output

I'd expect integer ranges to be carried through the generics.

@lcdennison lcdennison changed the title Integer ranges do not carry through generics Integer ranges in array keys do not carry through generics Apr 22, 2022
@ondrejmirtes ondrejmirtes added this to the Generics milestone Apr 22, 2022
@ondrejmirtes
Copy link
Member

Last night I came up with the idea that we mostly shouldn't generalize the generic type variables, except when they're in object generics, like Foo<int>.

Here's the resulting PR: phpstan/phpstan-src#2818

We can't do this for objects, because I want new Collection([1, 2, 3]) to become Collection<int>. This could be improved in the future thanks to this suggestion: #6732 (comment) (but it's pretty complex to implement so for now it has to wait).

The new behaviour now only applies to bleeding edge (https://phpstan.org/blog/what-is-bleeding-edge) so definitely enable it to get the taste of the future 👍

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

No branches or pull requests

2 participants