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

Type information lost during array key affectation ? #4165

Closed
greg0ire opened this issue Dec 1, 2020 · 12 comments
Closed

Type information lost during array key affectation ? #4165

greg0ire opened this issue Dec 1, 2020 · 12 comments
Labels
Milestone

Comments

@greg0ire
Copy link

greg0ire commented Dec 1, 2020

Bug report / Feature Request? I'm not sure…

It seems that under some conditions, PHPStan generalizes a "finite" (not sure what the right term is) type to an "infinite" one.

Code snippet that reproduces the problem

https://phpstan.org/r/1d06b068-483f-483e-a0cf-ef0ff3a29ade

Expected output

No errors.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 1, 2020
@ondrejmirtes
Copy link
Member

Yeah, it's a bug :) I'd start by removing the generalization from here https://github.com/phpstan/phpstan-src/blob/0e3230199e5282639bc50500286bc8244ff8a748/src/Type/Constant/ConstantArrayTypeBuilder.php#L101, it might make some tests fail, but it will probably fix this issue.

@binary-data
Copy link

I guess a similar thing happens there: https://phpstan.org/r/9e3f4300-ee94-4f03-98cf-cc5cfa329f15

PHPStan starts treating int as int|string

@ondrejmirtes
Copy link
Member

@binary-data This is completely different, please open a separate issue about that.

@binary-data
Copy link

Opened a new issue: #4612

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>&nonEmpty.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<non-empty-string, 3|4|5|42>&nonEmpty.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<literal-string&non-empty-string, 3|4|5|42>&nonEmpty.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<literal-string&non-empty-string, 3|4|5|42>&nonEmpty.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<literal-string&non-empty-string, 3|4|5|42>&nonEmpty.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns non-empty-array<literal-string&non-empty-string, 3|4|5|42>.
Full report
Line Error
33 `Method ComparisonKeysBuilder::__invoke() should return array<'int'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-33: Method ComparisonKeysBuilder::__invoke() should return array<'int'|'prd'|'stg', int> but returns array<string, 3|4|5|42>.
+No errors

@greg0ire greg0ire closed this as completed Jan 9, 2022
ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Jan 11, 2022
@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 Feb 10, 2022
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

4 participants