Skip to content

Swap template bounds in contravariant positions - #7208

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
template-bounds-variance
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
template-bounds-variance

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

First of four follow-ups to the template bound inference added in 01f99c1.

TemplateTypeMap tracks two bounds per template. The types from covariant positions are lower bounds: T[] receiving int[] means T :> int. The types from contravariant positions are upper bounds: callable(T): void receiving callable(int): void means T <: int. The second set was called lowerBoundTypes. It is upperBoundTypes now, and the class doc says which is which. union() and intersect() already combined each set correctly, so only the name changes there.

Two bugs are fixed:

/**
 * @template T
 * @param callable(callable(T): void): void $a
 * @param T $b
 * @return T
 */
function nestedCallable(callable $a, $b) {}

nestedCallable($callsWithIntCallback, 'a');
// was 'a', with a false argument.type:
//   expects callable(callable('a'): void): void, callable(callable(int): void): void given
// now 'a'|int

convertToLowerBoundTypes() moved both sets into the upper bounds instead of swapping them, so a template under two contravariant positions stayed an upper bound. The new swapBounds() swaps them.

/** @template-contravariant T */
interface Consumer {}

twoConsumers($intConsumer, $intOrStringConsumer);  // was int|string with a false argument.type, now int
consumerAndValues($intOrStringConsumer, [1]);       // was int|string, now 1

GenericObjectType::inferTemplateTypes() ignored variance, so Consumer<T> behaved like T[]. It now swaps the bounds of an argument whose template is contravariant, whether declared with @template-contravariant or at the call site (Box<contravariant T>). It works out the variance the same way getReferencedTemplateTypes() already does.

Backward compatibility (TemplateTypeMap is @api):

  • The constructor parameter keeps its name $lowerBoundTypes, so named arguments still work. Only the private property is renamed.
  • convertToLowerBoundTypes() stays as a @deprecated alias of swapBounds(). For a map without upper bounds, which is what inferTemplateTypes() 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 "expects callable(string, string): int, Closure(string, int) given" with "contains unresolvable type" on every parameter. TypeScript's getCommonSubtype() keeps the leftmost candidate for the same reason. The code now has a comment saying so.

The turbo mirrors of TemplateTypeMap and GenericObjectType::inferTemplateTypes(), and the swapBounds() call in the callable inference, are ported. smoke.php and type-family.php cover swapBounds(), 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-contravariant versions, and phpstan/phpstan#12444, where Invariant<T> extends Contravariant<T>. Each had false argument.type errors and now has none.

Closes phpstan/phpstan#13875
Closes phpstan/phpstan#12444

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
@ondrejmirtes
ondrejmirtes merged commit 1ea3eb3 into 2.3.x Nov 27, 2026
409 of 410 checks passed
@ondrejmirtes
ondrejmirtes deleted the template-bounds-variance branch November 27, 2026 09:55
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.

Contravariance: Parameters conflict with each other Interface with a contravariant template is not working as expected when extended

1 participant