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
Comments
Here an even simpler reproducer perhaps, coming from #5912 https://phpstan.org/r/409aaaa5-7094-4f16-aa5e-695e21a1a94b |
@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 |
@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
|
@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
|
Fixed: phpstan/phpstan-src@4321374 |
Closes phpstan/phpstan#5759 Closes phpstan/phpstan#5668 Closes phpstan/phpstan#5496 Closes phpstan/phpstan#4666 Closes phpstan/phpstan#3892 Closes phpstan/phpstan#3314 Closes phpstan/phpstan#2870 Closes phpstan/phpstan#5354
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. |
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-897f77d04e44https://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>
The text was updated successfully, but these errors were encountered: