Skip to content

Resolve a template contravariant in the return type to its upper bound - #7209

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
template-return-variance
Nov 27, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
template-return-variance

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Follow-up to #7208, which made TemplateTypeMap record lower bounds (from covariant positions) and upper bounds (from contravariant positions) correctly.

When a template has both bounds, the lower one always won. That is the most precise choice when the return type uses T covariantly. But when the return type uses T only in contravariant positions, a larger T gives the more precise return type:

/**
 * @template T
 * @param T $value
 * @param callable(T): void $handler
 * @return callable(T): void
 */
function handler($value, callable $handler): callable {}

$handler = handler(1, $intOrStringHandler);  // callable(int|string): void
// was callable(1): void, so passing it on gave a false argument.type:
takesHandler($handler);  // expects callable(int|string): void

GenericParametersAcceptorResolver::resolve() now resolves such a template to its upper bound. This is the rule from Pierce and Turner's local type inference, which picks a template's value by its variance in the result type. A template the return type uses covariantly, in both kinds of position (array{T, callable(T): void}), or not at all keeps the lower bound as before. The test file covers each of these as a control.

The return type is only inspected when the call inferred an upper bound, which keeps the common generic call as cheap as before.

On 2.3.x, the Consumer<T> case was sometimes already right: when the result is assigned to a variable that is later passed to a Consumer<int|string> parameter, the inference from later usage widens T. The new assertions are on the call expressions themselves, so they test the resolution directly.

TemplateTypeMap gets a new public method, getUpperBoundTypes(). It is ported to turbo and covered in smoke.php and type-family.php. The new tests fail without the source change. Analysis output is identical with the extension on and off.

A template inferred from both kinds of position, such as the T in
@PARAM T $value and @PARAM callable(T): void $handler, has a lower and an
upper bound, and the lower one always won. That is the most precise choice
when the return type uses T covariantly, but when it uses T only in
contravariant positions, as in @return callable(T): void, a larger T is
the more precise return type. handler(1, $intOrStringHandler) returned
callable(1): void, which a callable(int|string): void parameter rejected.

Such a template now resolves to its upper bound, the rule of Pierce and
Turner's local type inference. A template the return type uses
covariantly, in both kinds of position, or not at all keeps the lower
bound. TemplateTypeMap::getUpperBoundTypes() exposes the upper bounds.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c3c-cedc-7330-a083-cc986590888f
@ondrejmirtes
ondrejmirtes force-pushed the template-return-variance branch from dacbfdb to 03b7182 Compare November 27, 2026 10:40
@ondrejmirtes
ondrejmirtes merged commit 53c0b95 into 2.3.x Nov 27, 2026
391 of 392 checks passed
@ondrejmirtes
ondrejmirtes deleted the template-return-variance branch November 27, 2026 10:41
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.

1 participant