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

Property type narrowing incorrectly applied inside closures #3004

Closed
kelunik opened this issue Mar 21, 2020 · 1 comment
Closed

Property type narrowing incorrectly applied inside closures #3004

kelunik opened this issue Mar 21, 2020 · 1 comment
Labels

Comments

@kelunik
Copy link
Contributor

kelunik commented Mar 21, 2020

In the following example, $this->closed is narrowed to false below the if, but this assumption is wrong inside the closure, because the closure might be called at a different time where this narrowing no longer applies.

https://psalm.dev/r/74b9bb8d18

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/74b9bb8d18
<?php

class Queue {
    private bool $closed = false;
    
    /** @var callable[] */
    private array $callbacks = [];
    
    public function enqueue(string $value): void {
        if ($this->closed) {
            return;
        }
        
        $this->callbacks[] = function () use ($value): void {
            if ($this->closed) {
                print 'closed';
            } else {
                print $value;
            }
        };
    }
    
    public function invoke(): void {
        foreach ($this->callbacks as $callback) {
            $callback();
        }
    }
}
Psalm output (using commit c986cdf):

ERROR: TypeDoesNotContainType - 15:17 - if (false) is can never happen

@muglug muglug added the bug label Mar 21, 2020
@muglug muglug closed this as completed in 6746f1c Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants