Repository navigation
fix(templates): localize the return type of a closure in a used trait - #12810
Conversation
…imeo#7495 A closure's `@return T` inside a trait method was compared as the raw template of the trait, while the method's own return type is localized to what the using class gives it (`@use c<b>`), so returning a `b` was reported as an invalid return type and statement. The closure's declared return type is localized the same way, in both the return type verification and the return statements. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Behaviour change
|
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
✅ Performance: unchanged · Types: unchanged
How to read this table
head c7c9195 · base 67cfa84 (merge-base) · workflow run |
# Conflicts: # tests/Template/TraitTemplateTest.php
|
Checked the report's findings on the branch (merged with current master, Finding 1 not reproduced: a closure in a trait with Ran: probes on master and on the branch, the PR's cases on master. No code change pushed. |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 The change fixes the reported false positives for a closure in a trait and keeps genuinely wrong returns reported; I found no blocking issue.
Verified
- Current master reports
InvalidReturnTypeandInvalidReturnStatementfor the closure of #7495 (@return Tin a trait with@use c<b>); the branch reports nothing. - A closure that returns something that is not
b(@use W<b2>, closure returns an anonymousa2) is still reported, now againstb2instead ofT. - The same closure in a class with
@extends P<b2>already passes on master, so the change is only needed for traits (the methods of a trait are analysed per using class, so the closure'sThas the trait as defining class while the return statement is typed by the using class).
Recommendations (non-blocking)
- The description says "(or parent class)", but
closureReturnTypeNamesInheritedTemplatepasses on master; it is a guard, not a regression test for this change. Worth saying so, so that nobody reads it as covering a parent-class bug. localizeClosureReturnType()is called once inFunctionLikeAnalyzerand again for eachreturninReturnAnalyzer, and each call runsClassTemplateParamCollector::collect()+TemplateStandinTypeReplacer::replace(). It is gated ontemplate_extended_params, but computing the localized type once per closure analysis would avoid repeating it for every return.- Method templates in a closure's
@returnare not covered by the new test (only class/trait templates). If that path is intended to work, a case would pin it down.
|
From the fresh triage report (head 0e2bd9b), probed on the branch and on current master (
Not run: the full suite (CI). Nothing pushed for this comment. |
|
Question for the maintainers (@orklah @alies-dev), from the triage report generated 2026-11-19 05:16 UTC: Finding 1 of the report asks to localise the parameters and the return of a closure in one place (the storage of the closure when it is analysed for a class) instead of a second hook per consumer; here only the return type is localised (
I would keep A and merge the two PRs separately; which do you want? Nothing else of this PR waits on me. |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 Nothing was pushed since the last review (head 0e2bd9bc81), but a re-check of the branch merged into current master found a regression I missed the first time, so I can't call this mergeable yet.
Blocking
-
ReturnAnalyzer.php:348/FunctionLikeAnalyzer.php:951: the declared return type of the closure is now localised (Tbecomesb), but the type of what the closure returns is not. A closure whose@return Treturns a value that is itself typedT(a captured@param Tvariable, or@var T) is reported on this branch and is silent onmaster:interface a {} final class b implements a {} /** @template T of a */ trait e { /** @param T $p */ public function m($p): Closure { return /** @return T */ function () use ($p) { return $p; }; } } final class f { /** @use e<b> */ use e; } // master: no issue // this PR: MoreSpecificReturnType: The declared return type 'b' for ...:closure is more specific than the inferred return type 'T:e as a' // LessSpecificReturnStatement: The type 'T:e as a' is more general than the declared return type 'b'
The same happens with
/** @var T $t */ $t = ...; return $t;inside the closure. So the PR trades the original false positive (return $this->get()typedb, declaredT) for the mirror one in a shape that is just as common in a generic trait (a closure that hands back a templated value it captured). Localising the inferred type with the same helper is not enough on its own (I tried it onReturnAnalyzer: the statement type becomesb, butLessSpecificReturnStatementstill fires, "bis more general thanb"), so one option is to report only when the returned type fits neither the localised nor the original declared type. Whatever you pick, please add the snippet above toTraitTemplateTestas a valid case next toclosureReturnTypeNamesTraitTemplate.
Dropped / optional now
- Everything in the first review stays a non-blocking suggestion (the parent-class guard test wording, per-return recomputation, method templates in the closure
@return). - On your question about one shared localisation for parameters and return: not required for this PR. Keep it return-only and merge #13220 separately, as you proposed.
…in either terms, the trait's or the using class's Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @jirkace! |
Psalm reports
InvalidReturnTypeandInvalidReturnStatementfor a closure inside a trait method that declares@return T, when the using class bindsTwith@use.Before
After
Fixes #7495