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

Flipping a bool not detected #10566

Closed
WyriHaximus opened this issue Feb 12, 2024 · 10 comments
Closed

Flipping a bool not detected #10566

WyriHaximus opened this issue Feb 12, 2024 · 10 comments
Labels

Comments

@WyriHaximus
Copy link
Sponsor Contributor

Bug report

The bool PHPStan isn't fully understanding is used to stop the loop, and it called in a (nested) handler. Somehow PHPStan doesn't get that. (Not sure how to best word this honestly.)

Code snippet that reproduces the problem

https://phpstan.org/r/71e8fa68-3cbd-4258-9632-00f3a8dca770

Expected output

No error reported on line 19

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

No response

@ondrejmirtes
Copy link
Member

The problem is that PHPStan does not understand the closure is immediately called and the outer scope should be invalidated. If we call stop directly it works: https://phpstan.org/r/dda0a857-608f-4756-8ad3-e6269e0777e2

A discussion I had earlier today is highly relevant to this: #10565 (comment)

(We should have more information about what happens to a passed closure, it would be easier to analyse code with this information.)

@WyriHaximus
Copy link
Sponsor Contributor Author

The problem is that PHPStan does not understand the closure is immediately called and the outer scope should be invalidated. If we call stop directly it works: https://phpstan.org/r/dda0a857-608f-4756-8ad3-e6269e0777e2

It's sadly a bit more complicated than that, mainly made it simpler to show the issue. If you call it after the if in the loop the error returns: https://phpstan.org/r/8c849f4a-e1bc-4de1-bfa1-435202622159 Anything on lines 182, 183, and 212 might call a callable that will: https://github.com/reactphp/event-loop/blob/3.x/src/StreamSelectLoop.php#L181-L213

A discussion I had earlier today is highly relevant to this: #10565 (comment)

(We should have more information about what happens to a passed closure, it would be easier to analyse code with this information.)

If we could provide a callable MIGHT call stop that would help.

@ondrejmirtes
Copy link
Member

 the error returns

What's your expected output for https://phpstan.org/r/8c849f4a-e1bc-4de1-bfa1-435202622159? I think it's correct. You're breaking inside the if so directly after the if it's true. And after the stop() it's bool again.

@WyriHaximus
Copy link
Sponsor Contributor Author

the error returns

What's your expected output for https://phpstan.org/r/8c849f4a-e1bc-4de1-bfa1-435202622159? I think it's correct. You're breaking inside the if so directly after the if it's true. And after the stop() it's bool again.

Good point, you are right it is correct because it's 100% guaranteed to be called. However, if you add uncertainty to the mix it still reports the same error: https://phpstan.org/r/62cd712d-e2b4-4e26-92da-8338eecea1b2

@ondrejmirtes
Copy link
Member

I think the output is still correct?

@WyriHaximus
Copy link
Sponsor Contributor Author

In this simple form, probably. Want me to create an example closer to the code?

@ondrejmirtes
Copy link
Member

I haven't seen a real a PHPStan bug here so far so please do :)

@ondrejmirtes
Copy link
Member

Added void return types to the example so that it gets picked up by the issue bot once phpstan/phpstan-src#2980 is merged: https://phpstan.org/r/9e274e43-0153-42a7-8d52-56678966f813

@ondrejmirtes
Copy link
Member

Fixed phpstan/phpstan-src#2980

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 Apr 20, 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