Repository navigation
Treat invariant template arguments that are subtypes of each other as the same type - #7174
Merged
Merged
Conversation
… the same type TemplateTypeVariance compared invariant arguments with equals() only, so two arguments PHPStan proves to be subtypes of each other but writes differently - array<int>|object and iterable<int>|object - were rejected as "not covariant". Type equality is mutual subtyping; equals() stays the fast path. Unresolved template arguments (bleeding edge) stay opaque: they delegate isSuperTypeOf() but must never equal their delegate in an invariant position, so arguments containing one are compared with equals() only (UnresolvedTemplateArgumentFinder). turbo-ext: the same in TemplateTypeVariance.cpp; the finder is referenced as a TypeTraverserCallable. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
Legacy array shapes are open as subtypes and closed as supertypes, so
array{required: string} and array{required: string, optional?: string}
are subtypes of each other without bleeding edge. Mutual subtyping then
accepted Props<array{required: string}> for
Props<array{required: string, optional?: string}>, which the issue bot
caught on phpstan/phpstan#7813 (level 9).
With bleeding edge, shapes are sealed and the error stays; without it,
invariant arguments are compared with equals() as before.
turbo-ext: the same in TemplateTypeVariance.cpp.
Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
ondrejmirtes
force-pushed
the
invariant-mutual-subtyping
branch
from
November 26, 2026 05:53
6b5cb85 to
559035c
Compare
Type::hasUnresolvedTemplateArgumentType() walks the types a type holds the way hasTemplateOrLateResolvableType() does: an UnresolvedTemplateArgumentType answers yes, a late-resolvable type asks the type it resolves to, a template type its bound. It replaces UnresolvedTemplateArgumentFinder in TemplateTypeVariance. ClosureType keeps its memoized traversal as its implementation of the new method; containsUnresolvedTemplateArgument() is deprecated and delegates to it. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
ondrejmirtes
force-pushed
the
invariant-mutual-subtyping
branch
from
November 26, 2026 06:11
559035c to
9b8098f
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.
Invariant template arguments were compared with
equals()only, so arguments that PHPStan itself proves to be subtypes of each other, but that normalise to different descriptions, were rejected:Type equality is mutual subtyping (Scala 3's
isSameType, JLS);equals()stays as the fast path, and a proper subtype is still rejected with the covariance tip.Unresolved template arguments (bleeding edge) deliberately stay opaque in invariant positions - they delegate
isSuperTypeOf()but must never equal their delegate - so arguments containing one keep theequals()-only comparison. Whether a type holds one is answered by the newType::hasUnresolvedTemplateArgumentType(), which walks the types a type holds the wayhasTemplateOrLateResolvableType()does; it also replacesClosureType::containsUnresolvedTemplateArgument().Mutual subtyping applies only with bleeding edge. Legacy array shapes are open as subtypes and closed as supertypes, so
array{required: string}andarray{required: string, optional?: string}are subtypes of each other without it. The issue bot caught that on phpstan/phpstan#7813, where the correct error at level 9 disappeared. Without bleeding edge, invariant arguments are compared withequals()as before.TemplateTypeVarianceTest::testInvariantMutualSubtypescovers both modes.turbo-ext mirrors the change.