Skip to content

Variable written by a closure through use (&$x) is assumed to keep its initial null #13809

Closed

Description

@orklah
<?php
interface App { public function send(): void; }
interface Term { public function terminate(): void; }
final class R {
    public function __construct(private App $app) {}
    public function run(): void {
        $terminationException = null;
        $handler = function () use (&$terminationException): void {
            try {
                $this->app->send();
            } finally {
                if ($this->app instanceof Term) {
                    try {
                        $this->app->terminate();
                    } catch (\Throwable $e) {
                        $terminationException = $e;
                    }
                }
            }
        };
        do {
            $ret = handle_request($handler);
            if (null !== $terminationException) {
                throw $terminationException;
            }
        } while ($ret);
    }
}
function handle_request(callable $c): bool { return true; }

https://psalm.dev/r/008b447f82

Actual: TypeDoesNotContainType: Type null for $terminationException is always null, so its negation can never be true on line 23.
Expected: no issue. The closure captures $terminationException by reference and assigns it in its catch, so after handle_request($handler) it may hold a Throwable.

Found by comparing Psalm 6.20.11 and master on Symfony v8.1.11. Psalm 6.20.11 reports it too. Symfony location: Runtime/Runner/FrankenPhpWorkerRunner.php:86. The reverse case, a closure assuming the initial type, was #10670.

Activity

  1. psalm-github-bot commented on Nov 15, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/008b447f82
    <?php
    interface App { public function send(): void; }
    interface Term { public function terminate(): void; }
    final class R {
        public function __construct(private App $app) {}
        public function run(): void {
            $terminationException = null;
            $handler = function () use (&$terminationException): void {
                try {
                    $this->app->send();
                } finally {
                    if ($this->app instanceof Term) {
                        try {
                            $this->app->terminate();
                        } catch (\Throwable $e) {
                            $terminationException = $e;
                        }
                    }
                }
            };
            do {
                $ret = handle_request($handler);
                if (null !== $terminationException) {
                    throw $terminationException;
                }
            } while ($ret);
        }
    }
    function handle_request(callable $c): bool { return true; }
    Psalm output (using commit 9a75b79):
    
    INFO: UnusedParam - 29:34 - Param c is never referenced in this method
    
    ERROR: TypeDoesNotContainType - 23:17 - Type null for $terminationException is always null, so its negation can never be true
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions