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

Failed to infer iterable type when iterating on \Generator<someType>|null #5998

Closed
dktapps opened this issue Nov 12, 2021 · 2 comments
Closed

Comments

@dktapps
Copy link
Contributor

dktapps commented Nov 12, 2021

Bug report

I know that iterating on Generator|null is a bug, but it's not reported until level 7.

When up to level 6, PHPStan does not report a problem and should be able to understand what the iterable type will be in the case where it's not null.

Code snippet that reproduces the problem

https://phpstan.org/r/c9fc2ca5-501f-4fbe-bdcc-cf14a9f12ea9

Expected output

The dumped type should be a union of Block and *ERROR*. It may be an stdClass or it may not be iterable at all.

In addition, an error should be reported on line 18, because neither *ERROR* nor Block has a method called getPos() (definite crash).

Additional notes

I consider this a serious problem because it's an obvious bug that otherwise won't get detected until reaching level 8. This means that reaching level 6 provides a false sense of security while missing problems like this.

Did PHPStan help you today? Did it make you happy in any way?

It's greatly assisted me while updating an old project to a newer API.

@phpstan-bot
Copy link
Contributor

@dktapps After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
 10: Property HelloWorld::$nullableGenerator is never written, only read.
 13: Property HelloWorld::$regularGenerator is never written, only read.
-17: Dumped type: *ERROR*
+17: Dumped type: Block
+18: Call to an undefined method Block::getPos().
 22: Dumped type: Block
 23: Call to an undefined method Block::getPos().
Full report
Line Error
10 Property HelloWorld::$nullableGenerator is never written, only read.
13 Property HelloWorld::$regularGenerator is never written, only read.
17 Dumped type: Block
18 Call to an undefined method Block::getPos().
22 Dumped type: Block
23 Call to an undefined method Block::getPos().

@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 Jul 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants