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

Incorrect type refinement with in_array(..., strict: true) #5759

Closed
Firehed opened this issue Oct 8, 2021 · 6 comments
Closed

Incorrect type refinement with in_array(..., strict: true) #5759

Firehed opened this issue Oct 8, 2021 · 6 comments
Labels
Milestone

Comments

@Firehed
Copy link
Contributor

Firehed commented Oct 8, 2021

Bug report

Somewhere between 0.12.94 and 0.12.99, a correctly-reachable bit of code following an in_array(const-enum, array-of-const-enums, true) check started leading to incorrectly marking the value as unreachable. Using \PHPStan\dumpType($array) shows the correct set of possible values entering the function and conditional.

I suspect this is in some way related to the recent improvements which removed the hard limit of 8 values in constant scalar arrays, but haven't done a deep dive. It does occur (as demonstrated below) in smaller value sets, so possibly it's been a latent issue that only just started happening on my larger enums.

Update: poking around a bit more, I observed that without in_array(..., strict: true), I do not receive the unreachable code error. However, the checked value is still not refined out of the variable.

Code snippet that reproduces the problem

https://phpstan.org/r/2af7eca1-73d9-4489-a107-897f77d04e44

https://phpstan.org/r/437803d5-e64c-4149-9715-8805052141eb

Note the code I discovered this with is more complex (more constants, other conditionals, etc), but this seems to be the simplest reproduce case. The updated snippet reveals the issue is specific to strict checks of array contents.

Expected output

No "unreachable statement" error; dumpTypes should have Dumped type: array<2|3>

@Firehed Firehed changed the title Incorrect "unreachable statement" Incorrect type refinement with in_array(..., strict: true) Oct 8, 2021
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Oct 11, 2021
@Seldaek
Copy link
Contributor

Seldaek commented Nov 3, 2021

Here an even simpler reproducer perhaps, coming from #5912 https://phpstan.org/r/409aaaa5-7094-4f16-aa5e-695e21a1a94b

@phpstan-bot
Copy link
Contributor

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

@@ @@
-15: Unreachable statement - code above always terminates.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-15: Unreachable statement - code above always terminates.
+15: Dumped type: array<1|2|3>
 26: Dumped type: array<1|2|3>
Full report
Line Error
15 `Dumped type: array<1
26 `Dumped type: array<1

@phpstan-bot
Copy link
Contributor

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

@@ @@
-6: Dumped type: array<'dist'|'source'>
-7: Else branch is unreachable because ternary operator condition is always true.
+6: Dumped type: array<'dist'|'source'>
Full report
Line Error
6 `Dumped type: array<'dist'

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@4321374

@github-actions
Copy link

github-actions bot commented Apr 3, 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 Apr 3, 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