Skip to content

Commit 70e67c3

Browse files
committed
Look up the members of a generic object on its class without type arguments
GenericStaticType::getStaticObjectType() gives the GenericObjectType a class reflection that already carries the type arguments. Methods found on it had their template types resolved before the call, so a conditional return type merged with the native one too early: (false is true ? P : R)|P. Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
1 parent 14b2716 commit 70e67c3

6 files changed

Lines changed: 208 additions & 91 deletions

File tree

‎src/Type/Generic/GenericObjectType.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ private function isSuperTypeOfInternal(Type $type, bool $acceptsContext): IsSupe
222222
return $result;
223223
}
224224

225+
/**
226+
* The class reflection given to the constructor can already carry the type arguments
227+
* (GenericStaticType::getStaticObjectType() passes one), and its members would then
228+
* resolve the class's template types before they are called on this type.
229+
*/
230+
public function getNakedClassReflection(): ?ClassReflection
231+
{
232+
$classReflection = parent::getNakedClassReflection();
233+
if ($classReflection === null || !$classReflection->isGeneric()) {
234+
return $classReflection;
235+
}
236+
237+
return ReflectionProviderStaticAccessor::getInstance()->getClass($classReflection->getName());
238+
}
239+
225240
public function getClassReflection(): ?ClassReflection
226241
{
227242
if ($this->classReflection !== null) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace GenericStaticConditionalReturn;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class Promise
10+
{
11+
12+
}
13+
14+
class Result
15+
{
16+
17+
}
18+
19+
/**
20+
* @template TConcurrent of bool
21+
*/
22+
abstract class Base
23+
{
24+
25+
/** @return static<false> */
26+
public function sync(): static
27+
{
28+
throw new \Exception();
29+
}
30+
31+
/** @return (TConcurrent is true ? Promise : Result) */
32+
public function inherited(): Promise|Result
33+
{
34+
throw new \Exception();
35+
}
36+
37+
}
38+
39+
/**
40+
* @template TConcurrent of bool
41+
* @extends Base<TConcurrent>
42+
*/
43+
class Child extends Base
44+
{
45+
46+
/** @return (TConcurrent is true ? Promise : Result) */
47+
public function own(): Promise|Result
48+
{
49+
throw new \Exception();
50+
}
51+
52+
/** @return (TConcurrent is true ? 'promise' : 'result') */
53+
public function withoutObjects(): string
54+
{
55+
throw new \Exception();
56+
}
57+
58+
}
59+
60+
/**
61+
* @param Child<true> $concurrent
62+
* @param Child<false> $sync
63+
*/
64+
function test(Child $concurrent, Child $sync): void
65+
{
66+
assertType('GenericStaticConditionalReturn\Promise', $concurrent->own());
67+
assertType('GenericStaticConditionalReturn\Result', $sync->own());
68+
69+
// static<false> is the same Child<false> as the declared one
70+
assertType('GenericStaticConditionalReturn\Child<false>', $concurrent->sync());
71+
assertType('GenericStaticConditionalReturn\Result', $concurrent->sync()->own());
72+
assertType('GenericStaticConditionalReturn\Result', $concurrent->sync()->inherited());
73+
assertType('\'result\'', $concurrent->sync()->withoutObjects());
74+
}

‎turbo-ext/src/GenericObjectType.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ zend_class_entry *pt_ce_generic_object_type = nullptr;
4949

5050
/* the handlers the $this-dispatch fast paths identify */
5151
static void ZEND_FASTCALL gotGetClassReflection(INTERNAL_FUNCTION_PARAMETERS);
52+
static void ZEND_FASTCALL gotGetNakedClassReflection(INTERNAL_FUNCTION_PARAMETERS);
5253
static void ZEND_FASTCALL gotGetTypes(INTERNAL_FUNCTION_PARAMETERS);
5354
static void ZEND_FASTCALL gotRecreate(INTERNAL_FUNCTION_PARAMETERS);
5455
static void ZEND_FASTCALL gotGetUnresolvedPropertyPrototype(INTERNAL_FUNCTION_PARAMETERS);
@@ -424,6 +425,26 @@ class GenericObjectType
424425
return result;
425426
}
426427

428+
/* parent::getNakedClassReflection(), but the provider's reflection for
429+
* a generic class: the constructor's can already carry the type
430+
* arguments (GenericStaticType::getStaticObjectType() passes one);
431+
* null for an unknown class; UNDEF = pending exception */
432+
zv::Val getNakedClassReflection() const
433+
{
434+
zv::Val reflection = pt_object_type_get_naked_class_reflection(self);
435+
if (UNEXPECTED(reflection.isUndef())) return zv::Val();
436+
if (!zv::Ref(reflection.raw()).isObject()) return reflection;
437+
bool isGeneric;
438+
if (UNEXPECTED(!pt_type_call_bool(Z_OBJ_P(reflection.raw()), PT_LC("isgeneric"), 0, NULL, isGeneric))) return zv::Val();
439+
if (!isGeneric) return reflection;
440+
441+
zv::Val name = pt_type_call(Z_OBJ_P(reflection.raw()), PT_LC("getname"), 0, NULL);
442+
if (UNEXPECTED(name.isUndef())) return zv::Val();
443+
zv::Val provider = pt_reflection_provider_instance();
444+
if (UNEXPECTED(provider.isUndef())) return zv::Val();
445+
return pt_reflection_provider_get_class(Z_OBJ_P(provider.raw()), name.raw());
446+
}
447+
427448
/* the constructor's reflection, else the provider's parameterized with
428449
* the type arguments and variances (memoized); null for an unknown
429450
* class; UNDEF = pending exception */
@@ -1063,6 +1084,7 @@ static void pt_got_value_of(INTERNAL_FUNCTION_PARAMETERS, zv::Val (GenericObject
10631084
}
10641085

10651086
static void ZEND_FASTCALL gotGetClassReflection(INTERNAL_FUNCTION_PARAMETERS) { pt_got_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, &GenericObjectType::getClassReflection); }
1087+
static void ZEND_FASTCALL gotGetNakedClassReflection(INTERNAL_FUNCTION_PARAMETERS) { pt_got_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, &GenericObjectType::getNakedClassReflection); }
10661088
static void ZEND_FASTCALL gotGetTypes(INTERNAL_FUNCTION_PARAMETERS) { pt_got_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, &GenericObjectType::getTypes); }
10671089

10681090
static void ZEND_FASTCALL gotRecreate(INTERNAL_FUNCTION_PARAMETERS)
@@ -1126,6 +1148,7 @@ PT_MINIT_REGISTRATION(pt_register_generic_object_type)
11261148
});
11271149
cls.op<PT_OP_IS_SUPER_TYPE_OF, &GenericObjectType::isSuperTypeOf>();
11281150

1151+
cls.method(sigs::getNakedClassReflection, gotGetNakedClassReflection);
11291152
cls.method(sigs::getClassReflection, gotGetClassReflection);
11301153
cls.op<PT_OP_GET_CLASS_REFLECTION, &GenericObjectType::getClassReflection>();
11311154

‎turbo-ext/src/ObjectType.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,7 @@ void pt_object_type_construct(zend_object *self, zend_string *className, zval *s
33503350

33513351
zv::Val pt_object_type_describe(zend_object *self, zval *level) { return ObjectType(self).describe(level); }
33523352
zv::Val pt_object_type_get_class_reflection(zend_object *self) { return ObjectType(self).getClassReflection(); }
3353+
zv::Val pt_object_type_get_naked_class_reflection(zend_object *self) { return ObjectType(self).getNakedClassReflection(); }
33533354
bool pt_object_type_equals(zend_object *self, zval *type, bool &out) { return ObjectType(self).equals(type, out); }
33543355
zv::Val pt_object_type_get_referenced_classes(zend_object *self) { return ObjectType(self).getReferencedClasses(); }
33553356
zv::Val pt_object_type_is_super_type_of(zend_object *self, zval *type) { return ObjectType(self).isSuperTypeOf(type); }

‎turbo-ext/src/TypeTraits.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ void pt_object_type_construct(zend_object *self, zend_string *className, zval *s
749749
* UNDEF = pending exception (equals(): false = pending exception) */
750750
zv::Val pt_object_type_describe(zend_object *self, zval *level);
751751
zv::Val pt_object_type_get_class_reflection(zend_object *self);
752+
zv::Val pt_object_type_get_naked_class_reflection(zend_object *self);
752753
bool pt_object_type_equals(zend_object *self, zval *type, bool &out);
753754
zv::Val pt_object_type_get_referenced_classes(zend_object *self);
754755
zv::Val pt_object_type_is_super_type_of(zend_object *self, zval *type);

0 commit comments

Comments
 (0)