Skip to content

Commit bc12558

Browse files
ondrejmirtesclaude
andcommitted
Keep the lazy reasons of a maybe callable parameter comparison
CallableTypeHelper::isParametersAcceptorSuperTypeOf() rebuilds a maybe parameter result to append its "needs to be same or wider" reason, and passed on only the eager reasons. A maybe result can carry lazy reasons - a union parameter type's or() keeps those of its no members, like ConstantArrayType's reason for sealed shapes that cannot be intersected - so a reason that would have been listed had it been built eagerly was lost from getReasons(). The rebuilt result now keeps them; the native port does the same. (The accepts() path converts AcceptsResults, which have no lazy reasons, so nothing is lost there.) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 0d717ee commit bc12558

4 files changed

Lines changed: 82 additions & 10 deletions

File tree

‎src/Type/CallableTypeHelper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function isParametersAcceptorSuperTypeOf(
9191
$parameterDescription,
9292
$ourParameterType->describe($verbosity),
9393
),
94-
]));
94+
]), $isSuperType->lazyReasons);
9595
}
9696

9797
$result = $result->and($isSuperType);

‎tests/PHPStan/Type/CallableTypeTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,28 @@ public function testAccepts(
421421
);
422422
}
423423

424+
public function testIsSuperTypeOfKeepsLazyReasonsOfMaybeParameter(): void
425+
{
426+
$sealed = [new NeverType(true), new NeverType(true)];
427+
$acceptingParameterType = new ConstantArrayType([new ConstantStringType('a'), new ConstantStringType('b')], [new IntegerType(), new IntegerType()], optionalKeys: [1], unsealed: $sealed);
428+
$passedParameterType = new UnionType([
429+
new ConstantArrayType([new ConstantStringType('a')], [new IntegerType()], unsealed: $sealed),
430+
new ConstantArrayType([new ConstantStringType('b')], [new IntegerType()], unsealed: $sealed),
431+
]);
432+
$accepting = new CallableType([new NativeParameterReflection('x', false, $acceptingParameterType, PassedByReference::createNo(), false, null)], new VoidType());
433+
$passed = new CallableType([new NativeParameterReflection('x', false, $passedParameterType, PassedByReference::createNo(), false, null)], new VoidType());
434+
435+
// the passed parameter's isSuperTypeOf() is maybe, with a lazy reason from its array{b: int} member
436+
$parameterResult = $passedParameterType->isSuperTypeOf($acceptingParameterType);
437+
$this->assertTrue($parameterResult->maybe());
438+
$this->assertNotSame([], $parameterResult->lazyReasons);
439+
440+
$result = $accepting->isSuperTypeOf($passed);
441+
$this->assertTrue($result->maybe());
442+
$reasons = $result->getReasons();
443+
$this->assertCount(2, $reasons);
444+
$this->assertSame('Type array{a: int}|array{b: int} of parameter #1 $x of passed callable needs to be same or wider than parameter type array{a: int, b?: int} of accepting callable.', $reasons[0]);
445+
$this->assertStringStartsWith('Sealed array shapes array{b: int} and array{a: int, b?: int} cannot be intersected.', $reasons[1]);
446+
}
447+
424448
}

‎turbo-ext/src/CallableTypeHelper.cpp‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ class CallableTypeHelper
147147
ZSTR_VAL(Z_STR_P(parameterDescription.raw())),
148148
ZSTR_VAL(Z_STR_P(ourDescription.raw()))
149149
));
150-
/* new IsSuperTypeOfResult($isSuperType->result, array_merge($isSuperType->reasons, [$reason])) */
151-
zv::Val trinary, reasons;
150+
/* new IsSuperTypeOfResult($isSuperType->result, array_merge($isSuperType->reasons, [$reason]), $isSuperType->lazyReasons) */
151+
zv::Val trinary, reasons, lazyReasons;
152152
if (UNEXPECTED(!resultParts(isSuperType.raw(), trinary, reasons))) return zv::Val();
153+
if (UNEXPECTED(!lazyReasonsOf(isSuperType.raw(), lazyReasons))) return zv::Val();
153154
zv::Arr merged = zv::Arr::create(zv::ArrRef(reasons.raw()).size() + 1);
154155
if (UNEXPECTED(!pt_callable_array_merge_into(merged, reasons.raw()))) return zv::Val();
155156
merged.push(std::move(reason));
156-
isSuperType = create(trinary.raw(), std::move(merged));
157+
isSuperType = create(trinary.raw(), std::move(merged), std::move(lazyReasons));
157158
if (UNEXPECTED(isSuperType.isUndef())) return zv::Val();
158159
}
159160

@@ -218,18 +219,52 @@ class CallableTypeHelper
218219
return !result.isUndef();
219220
}
220221

221-
/* new IsSuperTypeOfResult($trinary, $reasons) ($reasons consumed);
222-
* UNDEF = pending exception */
223-
static zv::Val create(zval *trinary, zv::Arr reasons)
222+
/* new IsSuperTypeOfResult($trinary, $reasons, $lazyReasons) ($reasons
223+
* and $lazyReasons consumed, UNDEF $lazyReasons for none); UNDEF =
224+
* pending exception */
225+
static zv::Val create(zval *trinary, zv::Arr reasons, zv::Val lazyReasons = zv::Val())
224226
{
225227
zval out;
226228
zval reasonsRaw = reasons.take();
227-
zval lazyReasons;
228-
ZVAL_EMPTY_ARRAY(&lazyReasons);
229-
if (UNEXPECTED(!pt_result_object_create(&out, pt_ce_is_super_type_of_result, trinary, &reasonsRaw, &lazyReasons))) return zv::Val();
229+
zval lazyReasonsRaw;
230+
if (lazyReasons.isUndef()) {
231+
ZVAL_EMPTY_ARRAY(&lazyReasonsRaw);
232+
} else {
233+
lazyReasonsRaw = lazyReasons.take();
234+
}
235+
if (UNEXPECTED(!pt_result_object_create(&out, pt_ce_is_super_type_of_result, trinary, &reasonsRaw, &lazyReasonsRaw))) return zv::Val();
230236
return zv::Val::adopt(out);
231237
}
232238

239+
/* $result->lazyReasons of an IsSuperTypeOfResult — the slot of a native
240+
* one, the public property of the PHP twin declared next to it in the
241+
* differential tests; an owned copy, false = pending exception */
242+
[[nodiscard]] static bool lazyReasonsOf(zval *result, zv::Val &lazyReasons)
243+
{
244+
zend_object *object = Z_OBJ_P(result);
245+
if (EXPECTED(object->ce == pt_ce_is_super_type_of_result)) {
246+
zval *slot = OBJ_PROP_NUM(object, 2);
247+
if (UNEXPECTED(Z_TYPE_P(slot) != IS_ARRAY)) {
248+
zend_throw_error(NULL, "Typed property %s::$lazyReasons must not be accessed before initialization", ZSTR_VAL(object->ce->name));
249+
return false;
250+
}
251+
lazyReasons = zv::Val::copyOf(zv::Ref(slot));
252+
return true;
253+
}
254+
zval rv;
255+
zval *value = zend_read_property(object->ce, object, PT_LC("lazyReasons"), 0, &rv);
256+
if (UNEXPECTED(value == NULL || EG(exception))) return false;
257+
lazyReasons = zv::Val::copyOf(zv::Ref(value));
258+
if (value == &rv) {
259+
zval_ptr_dtor(&rv);
260+
}
261+
if (UNEXPECTED(!zv::Ref(lazyReasons.raw()).isArray())) {
262+
zend_type_error("phpstan_turbo: %s must carry an array $lazyReasons", ZSTR_VAL(object->ce->name));
263+
return false;
264+
}
265+
return true;
266+
}
267+
233268
/* new IsSuperTypeOfResult(TrinaryLogic::createNo(), [$reason]) ($reason
234269
* owned); UNDEF = pending exception */
235270
static zv::Val resultNo(zend_string *reason)

‎turbo-ext/tests/type-family.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8460,6 +8460,19 @@ public function getTypes(): array
84608460
$r["extra offset class $offsetClass"] = $misuse(static fn () => (new \PHPStan\Type\ObjectType($offsetClass))->isOffsetAccessible());
84618461
}
84628462

8463+
// a callable parameter compared maybe keeps the lazy reasons of the comparison
8464+
$sealedShape = [new \PHPStan\Type\NeverType(true), new \PHPStan\Type\NeverType(true)];
8465+
$acceptingShape = new \PHPStan\Type\Constant\ConstantArrayType([$cs('a'), $cs('b')], [new \PHPStan\Type\IntegerType(), new \PHPStan\Type\IntegerType()], [0], [1], null, $sealedShape);
8466+
$passedShapes = new \PHPStan\Type\UnionType([
8467+
new \PHPStan\Type\Constant\ConstantArrayType([$cs('a')], [new \PHPStan\Type\IntegerType()], [0], [], null, $sealedShape),
8468+
new \PHPStan\Type\Constant\ConstantArrayType([$cs('b')], [new \PHPStan\Type\IntegerType()], [0], [], null, $sealedShape),
8469+
]);
8470+
$shapeCallable = static fn (\PHPStan\Type\Type $parameterType) => new \PHPStan\Type\CallableType([new \PHPStan\Reflection\Native\NativeParameterReflection('x', false, $parameterType, \PHPStan\Reflection\PassedByReference::createNo(), false, null)], new \PHPStan\Type\VoidType());
8471+
foreach ([false, true] as $treatMixedAsAny) {
8472+
$helperResult = \PHPStan\Type\CallableTypeHelper::isParametersAcceptorSuperTypeOf($shapeCallable($acceptingShape), $shapeCallable($passedShapes), $treatMixedAsAny);
8473+
$r['callable helper lazy reasons ' . ($treatMixedAsAny ? 'any' : 'strict')] = [$helperResult->result->describe(), $helperResult->getReasons()];
8474+
}
8475+
84638476
// countConstantArrayValueTypes() hands each element to TypeTraverser::map(Type $type, ...)
84648477
foreach (['string' => 'x', 'object' => new \stdClass()] as $elementName => $element) {
84658478
$r["combinator countConstantArrayValueTypes $elementName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::countConstantArrayValueTypes([new \PHPStan\Type\IntegerType(), $element]));

0 commit comments

Comments
 (0)