Use lower bound types for contravariant template positions in GenericObjectType::inferTemplateTypes#5460
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…cObjectType::inferTemplateTypes` - In `GenericObjectType::inferTemplateTypes()`, inferred types from contravariant template positions are now converted to lower bound types via `convertToLowerBoundTypes()`. This mirrors how `ClosureType` handles parameter types (contravariant positions). - The effective variance is determined using the same logic as `getReferencedTemplateTypes()`: explicit call-site variance takes precedence, falling back to the declared template variance. - This fixes template inference when a child interface with an invariant template extends a parent with a contravariant template. Previously, types from contravariant positions were treated as upper bounds (unioned), causing the inferred type to widen incorrectly. Now they are treated as lower bounds (intersected), preserving the narrower type from covariant positions. - Also fixes the same issue for direct use of contravariant generic types in non-variadic parameters, methods, and static methods.
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.
Summary
When a function has a parameter like
Contravariant<T>(whereContravariantdeclares@template-contravariant T) and the argument is anInvariant<X>type (whereInvariantextendsContravariantwith an invariant template), PHPStan incorrectly inferred the function's templateTby treating types from the contravariant position as upper bounds (unioned). This caused the inferred type to widen to the broadest argument type, leading to false positiveargument.typeerrors.Changes
GenericObjectType::inferTemplateTypes()insrc/Type/Generic/GenericObjectType.phpto convert inferred template types to lower bounds when the template position is contravariantgetReferencedTemplateTypes(): checks explicit call-site variance first, then falls back to the declared template variance from the class reflectiontests/PHPStan/Analyser/nsrt/bug-12444.phpfor the reported issue (contravariant templates with direct and inherited types)tests/PHPStan/Analyser/nsrt/bug-12444b.phpcovering analogous cases:Root cause
GenericObjectType::inferTemplateTypes()always stored inferred types as upper bounds inTemplateTypeMap, regardless of the template's declared variance. When multiple arguments contribute to inferring the same template parameter:TypeCombinator::union()— this widens the typeTypeCombinator::intersect()— this narrows the typeFor contravariant positions, the correct behavior is to use lower bounds (intersection), matching how
ClosureType::inferTemplateTypesOnParametersAcceptor()already handles callable parameter types. Without this, when variadic args likeInvariant<Throwable>andInvariant<Exception>can't be simplified byTypeCombinator::union()(because invariant templates require exact match), the template inference producedT = Throwableinstead ofT = RuntimeException.Test
bug-12444.php: Tests the exact reproduction from the issue — contravariant template inference with both directContravariant<T>and inheritedInvariant<T> extends Contravariant<T>typesbug-12444b.php: Tests analogous cases — non-variadic params, mixed variance (@template-covariant+@template-contravariant), and methods/static methodsFixes phpstan/phpstan#12444