Skip to content

Uncaught TypeParseTreeException: an unparseable @self-out / @this-out type crashes the scan phase #11958

Closed

Description

@alies-dev

@self-out / @this-out is the only docblock tag in Psalm whose type is parsed without a try { } catch (TypeParseTreeException $e). Any type Psalm cannot parse there escapes as an uncaught throwable during the scan phase, killing the run before a single issue is reported. It cannot be suppressed, and <ignoreFiles> does not help because that skips analysis, not scanning.

laravel/framework v13.32.0 shipped the first widely installed docblock that hits this, so every Psalm 6 and Psalm 7 run against a project resolving laravel/framework >= 13.32 now dies outright. This holds even for projects whose own files never reference Laravel: composer's autoload files entries (Laravel's helpers) pull Collection.php into the scan phase.

Reproducer

repro.php:

<?php declare(strict_types=1);

final class A
{
    // 1. parameter-conditional type
    /** @phpstan-this-out ($key is null ? static : $this) */
    public function s(?int $key): void {}

    // 2. plain unparseable type — so this is not only about conditionals
    /** @psalm-self-out garbage<<< */
    public function t(): void {}
}

psalm.xml:

<?xml version="1.0"?>
<psalm errorLevel="1" xmlns="https://getpsalm.org/schema/config">
    <projectFiles>
        <directory name="."/>
    </projectFiles>
</psalm>

https://psalm.dev/r/80ce3d4b4a

Actual

Uncaught Psalm\Exception\TypeParseTreeException: Unrecognized template '$key' in .../src/Psalm/Internal/Type/TypeParser.php:342
Stack trace:
#0 .../src/Psalm/Internal/Type/TypeParser.php(281): Psalm\Internal\Type\TypeParser::getTypeFromTree()
#1 .../src/Psalm/Internal/Type/TypeParser.php(158): Psalm\Internal\Type\TypeParser::getTypeFromTree()
#2 .../src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php(319): Psalm\Internal\Type\TypeParser::parseTokens()
#3 .../src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php(495): Psalm\Internal\PhpVisitor\Reflector\FunctionLikeDocblockScanner::addDocblockInfo()
...
(Psalm 7.0.0-beta21@5df32a9 crashed due to an uncaught Throwable)

Case 2 crashes the same way, with Unexpected token < from ParseTreeCreator.

Expected

An InvalidDocblock issue, which is what every other tag in the same file already produces:

/** @return ($nope is null ? int : string) */  // InvalidDocblock: Unrecognized template '$nope' in docblock for A::r
/** @return garbage<<< */                       // InvalidDocblock: Unexpected token < in docblock for A::u
/** @param-out garbage<<< $x */                 // InvalidDocblock: Badly-formatted @param in docblock for A::v

Note that @return accepts the parameter-conditional form, because it routes the type through getConditionalSanitizedTypeTokens(), which rewrites $param tokens into generated TGeneratedFromParamN template params. The self_out path parses the raw tokens, so ConditionalTree looks $key up in the template map, does not find it, and throws.

Where

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner::addDocblockInfo(). Of the eight TypeParser::parseTokens() call sites in that file, the self_out and if_this_is blocks are the only two with no surrounding catch (TypeParseTreeException $e):

$ grep -n "TypeParser::parseTokens\|catch (TypeParseTreeException" src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php
257:                $storage->global_types[$global['name']] = TypeParser::parseTokens(
266:            } catch (TypeParseTreeException $e) {
319:            $out_type = TypeParser::parseTokens(     <-- self_out, unguarded
336:            $out_type = TypeParser::parseTokens(     <-- if_this_is, unguarded
614:            $namespaced_type = TypeParser::parseTokens(
628:        } catch (TypeParseTreeException $e) {
793:                $new_param_type = TypeParser::parseTokens(
806:            } catch (TypeParseTreeException $e) {
983:            $storage->return_type = TypeParser::parseTokens(
1039:        } catch (TypeParseTreeException $e) {
1180:            $removed_taint = TypeParser::parseTokens(
1197:        } catch (TypeParseTreeException $e) {
1387:            $out_type = TypeParser::parseTokens(
1398:        } catch (TypeParseTreeException $e) {
1451:                        $template_type = TypeParser::parseTokens(
1462:                    } catch (TypeParseTreeException $e) {

(Line numbers from 7.0.0-beta21; the structure is the same on 6.x.)

Making the parameter-conditional form actually work looks like a separate, larger change. Degrading to InvalidDocblock would already turn an unrecoverable crash into a suppressible issue.

Versions

  • Reproduced on 6.17.2, 7.0.0-beta20 and 7.0.0-beta21. Not a regression in any of them.
  • All three tag spellings crash: @self-out, @psalm-self-out, @phpstan-this-out.
  • PHP 8.4 and 8.5.

Real-world trigger

laravel/framework v13.32.0, Collection.php:1061, added in laravel/framework#61571:

 * @phpstan-this-out ($key is null ? static<TKey|int, TValue> : $this)

$key is a declared parameter of prepend(), so the docblock is well formed and PHPStan accepts it. Pinning laravel/framework below v13.32.0 is the only workaround I found.

Possibly related: #10126 (conditional @param-out silently ignored).

Activity

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

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/80ce3d4b4a
    <?php declare(strict_types=1);
    
    final class A
    {
        // 1. parameter-conditional type
        /** @phpstan-this-out ($key is null ? static : $this) */
        public function s(?int $key): void {}
    
        // 2. plain unparseable type — so this is not only about conditionals
        /** @psalm-self-out garbage<<< */
        public function t(): void {}
    }
    Psalm encountered an internal error:
    
    /vendor/vimeo/psalm/src/Psalm/Internal/Type/TypeParser.php: Unrecognized template '$key'
    
  2. added a commit that references this issue on Sep 23, 2026
    3228f4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions