Skip to content

Report InvalidDocblock for a @template whose as/of/super has no bound type - #15499

Open
shu-chen-dot-ca wants to merge 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13928-template-empty-bound
Open

shu-chen-dot-ca wants to merge 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13928-template-empty-bound

Conversation

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor

Psalm accepts @template T as (or of, super) with no bound type: TemplateTagParser::parse() only reads the keyword as a modifier when something follows it, so the dangling keyword becomes the template's description.

Before

<?php
/**
 * @template T as
 */
class A {} // no issue

/**
 * @template T as
 */
function foo() : void {} // no issue

After

class A {} // InvalidDocblock: Template missing as type

function foo() : void {} // InvalidDocblock: Template T missing as type

The parser now returns the modifier with an empty bound, which the class and function scanners already report. The same holds for of and super in any case, for the -covariant/-contravariant, @psalm- and @phpstan- forms, for methods, and for a keyword on the line after the name. Templates named of, as or super (@template of) stay valid.

noCrashOnInvalidClassTemplateAsType and noCrashOnInvalidFunctionTemplateAsType are re-enabled.

Fixes #13928

Before After
before after

@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 799.5 116.2 $\color{gray}{\textsf{+0.3\%}}$ 25 801 52 365 806 0
✅ symfony 3 632.0 190.8 $\color{gray}{\textsf{-0.6\%}}$ 52 331 100 811 1 775 0
✅ doctrine-orm 339.2 23.5 $\color{gray}{\textsf{+0.6\%}}$ 3 352 5 800 0 0
✅ psl 624.2 29.2 $\color{gray}{\textsf{+0.2\%}}$ 1 296 5 125 2 0
✅ phpunit 434.2 16.8 $\color{gray}{\textsf{+1.0\%}}$ 1 575 2 363 18 0
✅ php-parser 228.8 11.1 $\color{gray}{\textsf{+1.1\%}}$ 17 032 21 091 0 0
✅ tempest 915.4 31.3 5 494 11 032 271 0
✅ amphp-amp 48.7 1.7 $\color{gray}{\textsf{-0.6\%}}$ 30 57 0 0
✅ webmozarts-assert 282.7 5.6 $\color{gray}{\textsf{+0.5\%}}$ 576 2 740 0 0
✅ guzzle-promises 47.3 1.9 $\color{gray}{\textsf{+2.7\%}}$ 148 403 2 0
✅ monolog 140.3 6.7 $\color{gray}{\textsf{-1.9\%}}$ 937 2 483 31 0
✅ drupal 5 086.7 212.5 $\color{gray}{\textsf{-0.9\%}}$ 97 867 168 986 686 0
✅ wordpress 2 036.2 226.4 $\color{gray}{\textsf{+1.1\%}}$ 143 515 678 445 491 1
Total 15 615.3 873.8 349 954 1 051 701 4 082 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run side by side on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a second round was taken and the faster run of each side kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Profiling: when both sides support it, the measured runs use --debug-performance=PATH for the hot-files block, which adds the same small CPU and memory cost to both sides.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head 35f964b · base 55885bf (merge-base) · workflow run

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

bugbot review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 35f964b. Configure here.

@psalm-bot psalm-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status 🟡 Needs changes (1 should-fix)
Approach ✅ Root cause: template tag parsing in TemplateTagParser::parse()
Correctness ✅ No problems found: Empty bound reaches existing scanner checks; keyword-named templates unaffected.
Tests ⚠️ Bloated: Re-enabled cases prove the fix; three new invalid cases duplicate them.
Docs & comments ✅ OK: The @return description now covers an empty bound.

TemplateTagParser::parse() read as/of/super as a modifier only when another word followed it, so @template T as became a template named T with the keyword as its description and no bound. The PR now reads the keyword as a modifier whenever it follows the name, which yields an empty bound that ClassLikeNodeScanner and FunctionLikeDocblockScanner already report as Template ... missing as type. The fix is in the parser, where the keyword was misread, and templates named of/as/super still parse because the name is shifted off first. The fix is correct; the tests add three invalid cases that follow the same path as the two re-enabled ones.

Should fix (1)

Non-blocking suggestions (1)
  • tests/AnnotationTest.php:1099: templatesNamedLikeBoundKeywords needs only one template, e.g. @template of with @param of $a, @return of. All three names take the same $parts === [] path, so the other two templates and their params add nothing.

Automated review generated by AI; posted by psalm-bot.

Comment thread tests/AnnotationTest.php
Comment on lines +3576 to +3605
'classTemplateOfWithoutType' => [
'code' => '<?php
/**
* @template-covariant T of
*/
interface A {}',
'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:31'
. ' - Template missing as type',
],
'methodTemplateSuperWithoutType' => [
'code' => '<?php
final class A {
/**
* @psalm-template T SUPER
*/
public function foo() : void {}
}',
'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:25'
. ' - Template T missing as type',
],
'secondFunctionTemplateAsWithoutType' => [
'code' => '<?php
/**
* @template TFirst of int
* @template TSecond as
*/
function foo() : void {}',
'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:21'
. ' - Template TSecond missing as type',
],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should fix: The three new invalid cases follow the same code path as the two re-enabled ones.

The only behaviour change is the $parts !== [] check at TemplateTagParser.php:137. After that, every form ends at the same empty-bound branch: ClassLikeNodeScanner.php:620-624 for classes and FunctionLikeDocblockScanner.php:2187-2193 for functions and methods.

  • classTemplateOfWithoutType runs the same class path as noCrashOnInvalidClassTemplateAsType. of and -covariant were already handled: the keyword list is unchanged and the variance comes from the tag key in parseTags().
  • methodTemplateSuperWithoutType runs the same FunctionLikeDocblockScanner path as noCrashOnInvalidFunctionTemplateAsType. @psalm-template and the upper-case SUPER were already handled by parseTags() and strtolower().
  • secondFunctionTemplateAsWithoutType checks the per-template report, which this PR doesn't change.

Drop these three cases. To pin the message and location, move the pinned error_message strings into noCrashOnInvalidClassTemplateAsType and noCrashOnInvalidFunctionTemplateAsType, e.g. 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:27 - Template missing as type' for the class case. Adjust the column to where A sits in that snippet.

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, the parser change is right and the re-enabled cases fail on master for the right reason.

One problem on the class side, though: when a class template ends up with an empty bound, ClassLikeNodeScanner (around line 620) reports Template missing as type but then doesn't register the template at all, unlike FunctionLikeDocblockScanner, which falls back to mixed. Now that @template T as reaches that branch, a class using T gets a cascade of errors, and the following templates shift position:

<?php
/**
 * @template-covariant U of
 * @template V
 */
interface Pair {
    /** @return U */ public function first(): mixed;
    /** @return V */ public function second(): mixed;
}

/** @implements Pair<int, string> */
final class P implements Pair {
    public function first(): int { return 1; }
    public function second(): string { return 'a'; }
}

On this branch, besides the expected InvalidDocblock, this reports UndefinedDocblockClass for U, TooManyTemplateParams ... expecting 1 and ImplementedReturnTypeMismatch: The inherited return type 'int' for Pair::second, because V gets U's argument. Could you register the template as mixed in that else branch ($storage->template_types[$template_name] = [$fq_classlike_name => Type::getMixed()];), as the function scanner does, and add a case like the one above that expects only InvalidDocblock?

While you're at it, the bot's point on the tests still applies: the three new invalid cases go through the same path as the two re-enabled ones, so I'd drop them and pin the message and location in the re-enabled cases instead; that new class case would replace them nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@template T as with no bound type is accepted without InvalidDocblock

3 participants