Repository navigation
Swap template bounds in contravariant positions - #7208
Merged
Merged
Conversation
TemplateTypeMap tracks two bounds per template: the types inferred from covariant positions, which are lower bounds (T[] receiving int[] means T :> int), and the ones from contravariant positions, which are upper bounds (callable(T): void receiving callable(int): void means T <: int). The second set was called lowerBoundTypes; it is upperBoundTypes now. convertToLowerBoundTypes() moved both sets into the upper bounds instead of swapping them, so a template under two contravariant positions stayed an upper bound. In nestedCallable($cb, 'a') with @PARAM callable(callable(T): void): void, T was 'a' instead of int|'a', and the valid call was reported. swapBounds() swaps the two sets; convertToLowerBoundTypes() is kept as its deprecated alias. GenericObjectType::inferTemplateTypes() now swaps the bounds of an argument whose template is contravariant, declared with @template-contravariant or at the call site, as getReferencedTemplateTypes() already did. Consumer<T> receiving Consumer<int|string> made T a lower bound, so twoConsumers($intConsumer, $intOrStringConsumer) gave int|string and reported the Consumer<int> argument. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c3c-cedc-7330-a083-cc986590888f
This was referenced Nov 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First of four follow-ups to the template bound inference added in 01f99c1.
TemplateTypeMaptracks two bounds per template. The types from covariant positions are lower bounds:T[]receivingint[]meansT :> int. The types from contravariant positions are upper bounds:callable(T): voidreceivingcallable(int): voidmeansT <: int. The second set was calledlowerBoundTypes. It isupperBoundTypesnow, and the class doc says which is which.union()andintersect()already combined each set correctly, so only the name changes there.Two bugs are fixed:
convertToLowerBoundTypes()moved both sets into the upper bounds instead of swapping them, so a template under two contravariant positions stayed an upper bound. The newswapBounds()swaps them.GenericObjectType::inferTemplateTypes()ignored variance, soConsumer<T>behaved likeT[]. It now swaps the bounds of an argument whose template is contravariant, whether declared with@template-contravariantor at the call site (Box<contravariant T>). It works out the variance the same waygetReferencedTemplateTypes()already does.Backward compatibility (
TemplateTypeMapis@api):$lowerBoundTypes, so named arguments still work. Only the private property is renamed.convertToLowerBoundTypes()stays as a@deprecatedalias ofswapBounds(). For a map without upper bounds, which is whatinferTemplateTypes()returns, it gives the same result as before.Conflicting upper bounds still keep the first one. I tried storing never instead, but in
array_udiff(null, null, function (string $a, int $b))that replaced "expectscallable(string, string): int,Closure(string, int)given" with "contains unresolvable type" on every parameter. TypeScript'sgetCommonSubtype()keeps the leftmost candidate for the same reason. The code now has a comment saying so.The turbo mirrors of
TemplateTypeMapandGenericObjectType::inferTemplateTypes(), and theswapBounds()call in the callable inference, are ported.smoke.phpandtype-family.phpcoverswapBounds(), a double swap, the alias, and inference through a contravariant generic object. The new tests fail without the source change. Analysis output is identical with the extension on and off.Regression tests from the reporters' playground snippets: phpstan/phpstan#13875, both the call-site
MatchCondition<contravariant T>and the declaration-site@template-contravariantversions, and phpstan/phpstan#12444, whereInvariant<T>extendsContravariant<T>. Each had falseargument.typeerrors and now has none.Closes phpstan/phpstan#13875
Closes phpstan/phpstan#12444