Repository navigation
Resolve a template contravariant in the return type to its upper bound - #7209
Merged
Merged
Conversation
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
force-pushed
the
template-return-variance
branch
from
November 27, 2026 10:40
dacbfdb to
03b7182
Compare
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.
Follow-up to #7208, which made
TemplateTypeMaprecord 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
Tcovariantly. But when the return type usesTonly in contravariant positions, a largerTgives the more precise return type: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, theConsumer<T>case was sometimes already right: when the result is assigned to a variable that is later passed to aConsumer<int|string>parameter, the inference from later usage widensT. The new assertions are on the call expressions themselves, so they test the resolution directly.TemplateTypeMapgets a new public method,getUpperBoundTypes(). It is ported to turbo and covered insmoke.phpandtype-family.php. The new tests fail without the source change. Analysis output is identical with the extension on and off.