Skip to content

Commit 3fed264

Browse files
zonuexeclaude
authored andcommitted
Keep a narrowed float bound when creating a template type
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent b68353e commit 3fed264

5 files changed

Lines changed: 134 additions & 2 deletions

File tree

‎src/Type/Generic/TemplateTypeFactory.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public static function create(TemplateTypeScope $scope, string $name, ?Type $bou
8585
return new TemplateConstantIntegerType($scope, $strategy, $variance, $name, $bound, $default);
8686
}
8787

88-
if ($bound instanceof FloatType && ($boundClass === FloatType::class || $bound instanceof TemplateType)) {
88+
// Catches plain FloatType and the float subtypes that narrowing a bound
89+
// produces, a FloatRangeType or a ConstantFloatType, neither of which has a
90+
// dedicated Template* class to fall into.
91+
if ($bound instanceof FloatType) {
8992
return new TemplateFloatType($scope, $strategy, $variance, $name, $bound, $default);
9093
}
9194

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15714;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @template T of float
9+
* @param T $t
10+
* @return T
11+
*/
12+
function positive(float $t): float
13+
{
14+
if ($t > 0.0) {
15+
assertType('T of float<5.0E-324, max> (function Bug15714\positive(), argument)', $t);
16+
return $t;
17+
}
18+
19+
assertType('T of NAN|float<min, 0.0> (function Bug15714\positive(), argument)', $t);
20+
21+
return $t;
22+
}
23+
24+
/**
25+
* @template T of float
26+
* @param T $t
27+
* @return T
28+
*/
29+
function nanCheck(float $t): float
30+
{
31+
if (is_nan($t)) {
32+
assertType('T of NAN (function Bug15714\nanCheck(), argument)', $t);
33+
return $t;
34+
}
35+
36+
assertType('T of float<min, max> (function Bug15714\nanCheck(), argument)', $t);
37+
38+
return $t;
39+
}
40+
41+
/**
42+
* @template T of int
43+
* @param T $t
44+
* @return T
45+
*/
46+
function positiveInt(int $t): int
47+
{
48+
if ($t > 0) {
49+
assertType('T of int<1, max> (function Bug15714\positiveInt(), argument)', $t);
50+
return $t;
51+
}
52+
53+
assertType('T of int<min, 0> (function Bug15714\positiveInt(), argument)', $t);
54+
55+
return $t;
56+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,17 @@ public function testBug15245(): void
583583
]);
584584
}
585585

586+
public function testBug15714(): void
587+
{
588+
$this->checkNullables = true;
589+
$this->checkExplicitMixed = true;
590+
$this->analyse([__DIR__ . '/data/bug-15714.php'], [
591+
[
592+
'Function Bug15714ReturnType\\returnsFloat() should return T of float but returns float.',
593+
55,
594+
'Type float is not always the same as T. It breaks the contract for some argument types, typically subtypes.',
595+
],
596+
]);
597+
}
598+
586599
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15714ReturnType;
4+
5+
/**
6+
* @template T of float
7+
* @param T $t
8+
* @return T
9+
*/
10+
function positive(float $t): float
11+
{
12+
if ($t > 0.0) {
13+
return $t;
14+
}
15+
16+
return $t;
17+
}
18+
19+
/**
20+
* @template T of float
21+
* @param T $t
22+
* @return T
23+
*/
24+
function nanCheck(float $t): float
25+
{
26+
if (is_nan($t)) {
27+
return $t;
28+
}
29+
30+
return $t;
31+
}
32+
33+
/**
34+
* @template T of float
35+
* @param T $t
36+
* @return float
37+
*/
38+
function widened(float $t): float
39+
{
40+
if ($t > 0.0) {
41+
return $t;
42+
}
43+
44+
return $t;
45+
}
46+
47+
/**
48+
* @template T of float
49+
* @param T $t
50+
* @return T
51+
*/
52+
function returnsFloat(float $t, float $other): float
53+
{
54+
if ($t > 0.0) {
55+
return $other;
56+
}
57+
58+
return $t;
59+
}

‎turbo-ext/src/TemplateTypeFactory.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class TemplateTypeFactory
102102
return of(pt_template_constant_integer_type_new, scope, strategy, variance, nameStr, bound, defaultType);
103103
}
104104

105-
if (instanceof_function(boundClass, pt_ce_float_type) && (boundClass == pt_ce_float_type || isTemplate)) {
105+
/* Catches plain FloatType and the float subtypes that narrowing a bound produces, a FloatRangeType or a ConstantFloatType, neither of which has a dedicated Template* class to fall into — like the twin */
106+
if (instanceof_function(boundClass, pt_ce_float_type)) {
106107
return of(pt_template_float_type_new, scope, strategy, variance, nameStr, bound, defaultType);
107108
}
108109

0 commit comments

Comments
 (0)