Skip to content

Commit 0ed534e

Browse files
committed
Resolve an unconstrained template argument from all its bounds
A site with nothing inferred took its first covariant upper bound, so passing a fresh Box to a(Box<covariant int|string>) and then to b(Box<covariant int|bool>) inferred Box<int|string> and reported b(). Like JLS 18.4, lower bounds now come first, and failing them the argument is the intersection of every upper bound. Contradicting upper bounds keep the first one, so the conflicting call is still reported. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
1 parent 79cd194 commit 0ed534e

4 files changed

Lines changed: 122 additions & 5 deletions

File tree

‎src/Analyser/Generics/TemplateArgumentSolver.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private function resolveObservation(array $observation): Type
221221
// a covariant template already accepts every subtype - a known initial
222222
// type is never clamped
223223
if (!$templateVariance->covariant() || $acceptsAnything) {
224-
$covariantFallback = null;
224+
$upperBounds = [];
225225
$markerFallback = null;
226226
foreach ($observation['sends'] as [$sent, $variance]) {
227227
if ($variance->covariant()) {
@@ -236,7 +236,7 @@ private function resolveObservation(array $observation): Type
236236
$markerFallback ??= $sent;
237237
continue;
238238
}
239-
$covariantFallback ??= $this->substituteResolutions($sent);
239+
$upperBounds[] = $this->substituteResolutions($sent);
240240
continue;
241241
}
242242
$sent = $this->substituteResolutions($sent);
@@ -260,7 +260,17 @@ private function resolveObservation(array $observation): Type
260260
return $sent;
261261
}
262262

263-
if ($covariantFallback === null && $markerFallback !== null) {
263+
// like JLS 18.4: lower bounds are the most precise answer; failing
264+
// them, the argument must satisfy every upper bound - their
265+
// intersection, unless they contradict each other, in which case
266+
// the first one stands and the second pass reports the others
267+
$covariantFallback = null;
268+
if ($lowerBounds === [] && $upperBounds !== []) {
269+
$covariantFallback = TypeCombinator::intersect(...$upperBounds);
270+
if ($covariantFallback instanceof NeverType) {
271+
$covariantFallback = $upperBounds[0];
272+
}
273+
} elseif ($lowerBounds === [] && $markerFallback !== null) {
264274
$covariantFallback = $this->substituteResolutions($markerFallback);
265275
}
266276
if ($covariantFallback !== null) {

‎tests/PHPStan/Analyser/nsrt/template-argument-multiple-sites.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ function invariantIntoCovariant(): void
216216
$b->add(2);
217217
$z = freezeColls($a, $b);
218218
takesBoxOfInt($z);
219-
assertType('TemplateArgumentMultipleSites\Coll<int>', $a);
220-
assertType('TemplateArgumentMultipleSites\Box<int>', $z);
219+
assertType('TemplateArgumentMultipleSites\Coll<1|2>', $a);
220+
assertType('TemplateArgumentMultipleSites\Box<1|2>', $z);
221221
}
222222

223223
function contravariant(): void
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TemplateArgumentUpperBounds;
4+
5+
use Countable;
6+
use Traversable;
7+
use function PHPStan\Testing\assertType;
8+
9+
/** @template T */
10+
final class Box
11+
{
12+
13+
/** @param T $item */
14+
public function add($item): void
15+
{
16+
}
17+
18+
}
19+
20+
/** @param Box<covariant int|string> $b */
21+
function intOrString(Box $b): void
22+
{
23+
}
24+
25+
/** @param Box<covariant int|bool> $b */
26+
function intOrBool(Box $b): void
27+
{
28+
}
29+
30+
/** @param Box<covariant bool> $b */
31+
function onlyBool(Box $b): void
32+
{
33+
}
34+
35+
/** @param Box<contravariant int> $b */
36+
function intSink(Box $b): void
37+
{
38+
}
39+
40+
/** @param Box<covariant Countable> $b */
41+
function countable(Box $b): void
42+
{
43+
}
44+
45+
/** @param Box<covariant Traversable<mixed>> $b */
46+
function traversable(Box $b): void
47+
{
48+
}
49+
50+
function upperBounds(): void
51+
{
52+
$box = new Box();
53+
intOrString($box);
54+
intOrBool($box);
55+
assertType('TemplateArgumentUpperBounds\Box<int>', $box);
56+
}
57+
58+
function upperBoundsSwapped(): void
59+
{
60+
$box = new Box();
61+
intOrBool($box);
62+
intOrString($box);
63+
assertType('TemplateArgumentUpperBounds\Box<int>', $box);
64+
}
65+
66+
function objectUpperBounds(): void
67+
{
68+
$box = new Box();
69+
countable($box);
70+
traversable($box);
71+
assertType('TemplateArgumentUpperBounds\Box<Countable&Traversable<mixed, mixed>>', $box);
72+
}
73+
74+
function lowerBoundWithinUpperBound(): void
75+
{
76+
$box = new Box();
77+
intOrString($box);
78+
intSink($box);
79+
assertType('TemplateArgumentUpperBounds\Box<int>', $box);
80+
}
81+
82+
function addedWithinUpperBound(): void
83+
{
84+
$box = new Box();
85+
intOrString($box);
86+
$box->add(1);
87+
$box->add('x');
88+
assertType("TemplateArgumentUpperBounds\Box<1|'x'>", $box);
89+
}
90+
91+
function contradictingUpperBounds(): void
92+
{
93+
$box = new Box();
94+
intOrString($box);
95+
onlyBool($box);
96+
assertType('TemplateArgumentUpperBounds\Box<int|string>', $box);
97+
}

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public function testJointTemplateInferenceErrors(): void
9090
]);
9191
}
9292

93+
public function testTemplateArgumentUpperBounds(): void
94+
{
95+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/template-argument-upper-bounds.php'], [
96+
[
97+
'Parameter #1 $b of function TemplateArgumentUpperBounds\\onlyBool expects TemplateArgumentUpperBounds\\Box<covariant bool>, TemplateArgumentUpperBounds\\Box<int|string> given.',
98+
95,
99+
],
100+
]);
101+
}
102+
93103
public function testTemplateArgumentReduceIterator(): void
94104
{
95105
$this->analyse([__DIR__ . '/data/template-argument-reduce-iterator.php'], []);

0 commit comments

Comments
 (0)