Skip to content

Commit 42f0dd5

Browse files
miguelmeloninnatialondrejmirtes
authored andcommitted
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 5e6d8c4 commit 42f0dd5

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
@@ -799,13 +799,16 @@ class GenericObjectType
799799
/* Gives $subclass, a class written without type arguments, the ones
800800
* $supertype implies for it through the class's `@extends` and
801801
* `@implements` tags: Some with Option<int> is Some<int>, Err with
802-
* Result<int, string> is Err<string>.
802+
* Result<int, string> is Err<string>. The class of $supertype itself
803+
* takes its arguments and call-site variance as written: X with X<*> is
804+
* X<*>.
803805
*
804806
* Returns $subclass unchanged (the same object) unless $supertype is a
805-
* generic object type without call-site variance, $subclass is a
806-
* generic subtype of its class, and $supertype determines every type
807-
* argument of $subclass - an explicit argument would claim more than is
808-
* known. UNDEF = pending exception */
807+
* generic object type and $subclass is its class or a generic subtype
808+
* of it. A subtype also stays unchanged when $supertype has call-site
809+
* variance or does not determine every type argument of the subtype -
810+
* an explicit argument would claim more than is known. UNDEF = pending
811+
* exception */
809812
static zv::Val specializeSubclass(zval *supertype, zval *subclass)
810813
{
811814
/* $subclass is exactly an ObjectType: the shadowing class carries
@@ -814,8 +817,23 @@ class GenericObjectType
814817
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));
815818
zend_object *supertypeObject = Z_OBJ_P(supertype);
816819

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

0 commit comments

Comments
 (0)