Skip to content

Commit df57b6c

Browse files
Narrow a class-string with call-site variance by its class name
A class name compared with a class-string of its own generic class now takes the type arguments and the call-site variance of that class-string as they are written: `X::class` read against `class-string<X<*>>` is `X<*>`. So `$class !== X::class` narrows `class-string<X<*>>|class-string<Y<*>>` to `class-string<Y<*>>`. Before, a class-string with call-site variance left the class name bare, which is only maybe an `X<*>`, and nothing narrowed. A subclass still stays bare when the class-string has call-site variance.
1 parent 98c9c52 commit df57b6c

3 files changed

Lines changed: 90 additions & 11 deletions

File tree

‎src/Type/Generic/GenericObjectType.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,19 +464,32 @@ public function changeSubtractedType(?Type $subtractedType): Type
464464
* Gives $subclass, a class written without type arguments, the ones
465465
* $supertype implies for it through the class's `@extends` and
466466
* `@implements` tags: Some with Option<int> is Some<int>, Err with
467-
* Result<int, string> is Err<string>.
467+
* Result<int, string> is Err<string>. The class of $supertype itself
468+
* takes its arguments and call-site variance as written: X with X<*> is
469+
* X<*>.
468470
*
469471
* Returns $subclass unchanged unless $supertype is a generic object type
470-
* without call-site variance, $subclass is a generic subtype of its class,
471-
* and $supertype determines every type argument of $subclass - an explicit
472-
* argument would claim more than is known.
472+
* and $subclass is its class or a generic subtype of it. A subtype also
473+
* stays unchanged when $supertype has call-site variance or does not
474+
* determine every type argument of the subtype - an explicit argument
475+
* would claim more than is known.
473476
*/
474477
public static function specializeSubclass(Type $supertype, Type $subclass): Type
475478
{
476479
if (!$supertype instanceof self || get_class($subclass) !== ObjectType::class) {
477480
return $subclass;
478481
}
479482

483+
if ($subclass->getClassName() === $supertype->getClassName()) {
484+
return new self(
485+
$supertype->getClassName(),
486+
$supertype->types,
487+
$subclass->getSubtractedType(),
488+
null,
489+
$supertype->variances,
490+
);
491+
}
492+
480493
foreach ($supertype->variances as $variance) {
481494
if (!$variance->invariant()) {
482495
return $subclass;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug15266;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @template T = mixed */
8+
final class X {}
9+
10+
/** @template T = mixed */
11+
final class Y {}
12+
13+
/**
14+
* @param class-string<X<int>> | class-string<Y<int>> $class
15+
* @return class-string<X<int>> | class-string<Y<int>>
16+
*/
17+
function parametrized(string $class): string
18+
{
19+
if ($class !== X::class) {
20+
assertType('class-string<Bug15266\Y<int>>', $class);
21+
}
22+
23+
return $class;
24+
}
25+
26+
/**
27+
* @param class-string<X<*>> | class-string<Y<*>> $class
28+
* @return class-string<X<*>> | class-string<Y<*>>
29+
*/
30+
function star(string $class): string
31+
{
32+
if ($class !== X::class) {
33+
assertType('class-string<Bug15266\Y<*>>', $class);
34+
}
35+
36+
return $class;
37+
}
38+
39+
/**
40+
* @param class-string<X> | class-string<Y> $class
41+
* @return class-string<X> | class-string<Y>
42+
*/
43+
function raw(string $class): string
44+
{
45+
if ($class !== X::class) {
46+
assertType('class-string<Bug15266\Y>', $class);
47+
}
48+
49+
return $class;
50+
}

‎turbo-ext/src/GenericObjectType.cpp‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,16 @@ class GenericObjectType
798798
/* Gives $subclass, a class written without type arguments, the ones
799799
* $supertype implies for it through the class's `@extends` and
800800
* `@implements` tags: Some with Option<int> is Some<int>, Err with
801-
* Result<int, string> is Err<string>.
801+
* Result<int, string> is Err<string>. The class of $supertype itself
802+
* takes its arguments and call-site variance as written: X with X<*> is
803+
* X<*>.
802804
*
803805
* Returns $subclass unchanged (the same object) unless $supertype is a
804-
* generic object type without call-site variance, $subclass is a
805-
* generic subtype of its class, and $supertype determines every type
806-
* argument of $subclass - an explicit argument would claim more than is
807-
* known. UNDEF = pending exception */
806+
* generic object type and $subclass is its class or a generic subtype
807+
* of it. A subtype also stays unchanged when $supertype has call-site
808+
* variance or does not determine every type argument of the subtype -
809+
* an explicit argument would claim more than is known. UNDEF = pending
810+
* exception */
808811
static zv::Val specializeSubclass(zval *supertype, zval *subclass)
809812
{
810813
/* $subclass is exactly an ObjectType: the shadowing class carries
@@ -813,8 +816,23 @@ class GenericObjectType
813816
if (!zv::Ref(supertype).instanceOf(pt_ce_generic_object_type) || Z_OBJCE_P(subclass) != pt_ce_object_type) return zv::Val::copyOf(zv::Ref(subclass));
814817
zend_object *supertypeObject = Z_OBJ_P(supertype);
815818

819+
/* $subclass->getClassName() — the slot, the object is exactly the
820+
* native class */
821+
zend_string *subclassName = pt_object_type_class_name(Z_OBJ_P(subclass));
822+
if (UNEXPECTED(subclassName == NULL)) return zv::Val();
823+
zv::Val supertypeClassName = pt_type_call(supertypeObject, PT_LC("getclassname"), 0, NULL);
824+
if (UNEXPECTED(supertypeClassName.isUndef())) return zv::Val();
816825
zval *variances = variancesOf(supertypeObject);
817826
if (UNEXPECTED(variances == NULL)) return zv::Val();
827+
if (zv::Ref(supertypeClassName.raw()).isString() && zend_string_equals(subclassName, Z_STR_P(supertypeClassName.raw()))) {
828+
zval *types = typesOf(supertypeObject);
829+
if (UNEXPECTED(types == NULL)) return zv::Val();
830+
zval *subtractedType = pt_object_type_subtracted_type(Z_OBJ_P(subclass));
831+
if (UNEXPECTED(subtractedType == NULL)) return zv::Val();
832+
zend_string *supertypeName = Z_STR_P(supertypeClassName.raw());
833+
return create(supertypeName, types, Z_TYPE_P(subtractedType) == IS_NULL ? NULL : subtractedType, NULL, variances);
834+
}
835+
818836
for (zv::ArrayEntry entry : zv::ArrRef(variances)) {
819837
zv::Ref variance = entry.value().deref();
820838
if (UNEXPECTED(!variance.isObject())) {
@@ -857,8 +875,6 @@ class GenericObjectType
857875
}
858876
zv::Val bare = create(name.get(), templateTypeList.raw(), NULL, NULL, NULL);
859877
if (UNEXPECTED(bare.isUndef())) return zv::Val();
860-
zv::Val supertypeClassName = pt_type_call(supertypeObject, PT_LC("getclassname"), 0, NULL);
861-
if (UNEXPECTED(supertypeClassName.isUndef())) return zv::Val();
862878
zv::Val ancestor = pt_type_call(Z_OBJ_P(bare.raw()), PT_LC("getancestorwithclassname"), 1, supertypeClassName.raw());
863879
if (UNEXPECTED(ancestor.isUndef())) return zv::Val();
864880
if (ancestor.isNull()) return zv::Val::copyOf(zv::Ref(subclass));

0 commit comments

Comments
 (0)