Skip to content

Commit 416fce9

Browse files
committed
Bind only templates the parameters do not infer from a conditional return type
`($x is T ? A : B)` in the return type with T inferred from `T $t` no longer widens T with the argument for $x, which made the condition always hold: `same(1, 'a')` was `'same'`, while the negated form gave `'diff'`. Templates only the condition mentions, like T in `($id is class-string<T> ? T : mixed)`, are still bound by it. Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
1 parent fbccba8 commit 416fce9

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎src/Reflection/GenericParametersAcceptorResolver.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
286286
) {
287287
$paramType = $predicateTypeMap === null ? $returnType->getTarget() : self::resolvePredicateTemplateTypes($returnType->getTarget(), $predicateTypeMap);
288288
$argType = $passedArgs[$returnType->getParameterName()];
289-
$typeMap = $typeMap->union($paramType->inferTemplateTypes($argType));
289+
// the condition binds only the templates the parameters say nothing about, as T in
290+
// `($id is class-string<T> ? T : mixed)`; T inferred from `T $t` is not widened by
291+
// what the argument for the condition is, or `($x is T ? …)` would always hold
292+
$conditionTypeMap = $paramType->inferTemplateTypes($argType);
293+
foreach (array_keys($typeMap->getTypes()) as $name) {
294+
$conditionTypeMap = $conditionTypeMap->unsetType($name);
295+
}
296+
$typeMap = $typeMap->union($conditionTypeMap);
290297
}
291298

292299
$resolvedTemplateTypeMap = new TemplateTypeMap(array_merge(
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php // lint >= 8.0
2+
3+
namespace ConditionalReturnTypeTemplateBinding;
4+
5+
use stdClass;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Foo
9+
{
10+
11+
}
12+
13+
/**
14+
* @template T
15+
* @param T $t
16+
* @return ($x is T ? 'same' : 'diff')
17+
*/
18+
function same($t, mixed $x)
19+
{
20+
}
21+
22+
/**
23+
* @template T
24+
* @param T $t
25+
* @return ($x is not T ? 'diff' : 'same')
26+
*/
27+
function notSame($t, mixed $x)
28+
{
29+
}
30+
31+
/**
32+
* @template T of object
33+
* @param T $default
34+
* @return ($id is class-string<T> ? T : 'other')
35+
*/
36+
function getOr(string $id, object $default)
37+
{
38+
}
39+
40+
/**
41+
* Only the condition binds T.
42+
*
43+
* @template T of object
44+
* @return ($id is class-string<T> ? T : 'other')
45+
*/
46+
function get(string $id)
47+
{
48+
}
49+
50+
function (int $int): void {
51+
// T is inferred from $t, so the condition does not widen it with what $x is
52+
assertType('\'diff\'', same(1, 'a'));
53+
assertType('\'same\'', same(1, 1));
54+
assertType('\'same\'', same($int, 5));
55+
assertType('\'diff\'', notSame(1, 'a'));
56+
assertType('\'same\'', notSame(1, 1));
57+
58+
assertType('\'other\'', getOr(Foo::class, new stdClass()));
59+
assertType('ConditionalReturnTypeTemplateBinding\Foo', getOr(Foo::class, new Foo()));
60+
61+
assertType('ConditionalReturnTypeTemplateBinding\Foo', get(Foo::class));
62+
assertType('stdClass', get(stdClass::class));
63+
};

0 commit comments

Comments
 (0)