Skip to content

Treat invariant template arguments that are subtypes of each other as the same type - #7174

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
invariant-mutual-subtyping
Nov 26, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
invariant-mutual-subtyping

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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:

/** @param \ArrayObject<int, iterable<int>|object> $a */
function take(\ArrayObject $a): void {}

/** @param \ArrayObject<int, array<int>|object> $b */
function f(\ArrayObject $b): void {
	take($b); // expects ArrayObject<int, iterable<int>|object>, ArrayObject<int, array<int>|object> given
}

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 the equals()-only comparison. Whether a type holds one is answered by the new Type::hasUnresolvedTemplateArgumentType(), which walks the types a type holds the way hasTemplateOrLateResolvableType() does; it also replaces ClosureType::containsUnresolvedTemplateArgument().

Mutual subtyping applies only with bleeding edge. 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 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 with equals() as before. TemplateTypeVarianceTest::testInvariantMutualSubtypes covers both modes.

turbo-ext mirrors the change.

… 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
ondrejmirtes force-pushed the invariant-mutual-subtyping branch from 6b5cb85 to 559035c Compare November 26, 2026 05:53
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
ondrejmirtes force-pushed the invariant-mutual-subtyping branch from 559035c to 9b8098f Compare November 26, 2026 06:11
@ondrejmirtes
ondrejmirtes merged commit 7fe21c2 into 2.3.x Nov 26, 2026
427 of 435 checks passed
@ondrejmirtes
ondrejmirtes deleted the invariant-mutual-subtyping branch November 26, 2026 06:22
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