Skip to content

Commit ca825b0

Browse files
zonuexeclaude
authored andcommitted
Take the absolute value of a float template type from its bound
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3fed264 commit ca825b0

7 files changed

Lines changed: 171 additions & 1 deletion

File tree

‎src/Type/Generic/TemplateFloatType.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ public function __construct(
3737
$this->default = $default;
3838
}
3939

40+
/** The absolute value of a T is not a T, and FloatType's own answer, $this, would claim it is. */
41+
public function toAbsoluteNumber(): Type
42+
{
43+
return $this->getBound()->toAbsoluteNumber();
44+
}
45+
4046
}

‎tests/PHPStan/Analyser/nsrt/bug-15714.php‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,110 @@ function positiveInt(int $t): int
5454

5555
return $t;
5656
}
57+
58+
/**
59+
* @template T of float
60+
* @param T $t
61+
* @return T
62+
*/
63+
function absolute(float $t): float
64+
{
65+
assertType('float', abs($t));
66+
67+
if ($t < 0.0) {
68+
assertType('T of float<min, -5.0E-324> (function Bug15714\absolute(), argument)', $t);
69+
assertType('float<5.0E-324, max>', abs($t));
70+
}
71+
72+
return $t;
73+
}
74+
75+
/**
76+
* @template T of int
77+
* @param T $t
78+
* @return T
79+
*/
80+
function absoluteInt(int $t): int
81+
{
82+
assertType('int<0, max>', abs($t));
83+
84+
if ($t < 0) {
85+
assertType('int<0, max>', abs($t));
86+
}
87+
88+
return $t;
89+
}
90+
91+
/**
92+
* @template T of float
93+
* @param T $t
94+
* @return T
95+
*/
96+
function constant(float $t): float
97+
{
98+
if ($t >= 1.5 && $t <= 1.5) {
99+
assertType('T of 1.5 (function Bug15714\constant(), argument)', $t);
100+
return $t;
101+
}
102+
103+
return $t;
104+
}
105+
106+
/**
107+
* @template T of float
108+
* @param T $t
109+
* @return T
110+
*/
111+
function merged(float $t): float
112+
{
113+
$sign = $t >= 0.0 ? 'positive' : 'negative';
114+
assertType('T of float<0.0, max> (function Bug15714\merged(), argument)|(T of NAN|float<min, -5.0E-324> (function Bug15714\merged(), argument))', $t);
115+
116+
return $t;
117+
}
118+
119+
/**
120+
* @template T of float|int
121+
* @param T $t
122+
* @return T
123+
*/
124+
function unionBound($t)
125+
{
126+
if (is_float($t) && $t > 0.0) {
127+
assertType('T of float<5.0E-324, max> (function Bug15714\unionBound(), argument)', $t);
128+
return $t;
129+
}
130+
131+
return $t;
132+
}
133+
134+
/**
135+
* @template T of float<0, 1>
136+
* @param T $t
137+
* @return T
138+
*/
139+
function rangeBound(float $t): float
140+
{
141+
assertType('T of float<0.0, 1.0> (function Bug15714\rangeBound(), argument)', $t);
142+
assertType('float<0.0, 1.0>', abs($t));
143+
144+
return $t;
145+
}
146+
147+
/**
148+
* @template T of 1.5
149+
* @param T $t
150+
* @return T
151+
*/
152+
function constantBound(float $t): float
153+
{
154+
assertType('T of 1.5 (function Bug15714\constantBound(), argument)', $t);
155+
156+
return $t;
157+
}
158+
159+
function explicitBounds(): void
160+
{
161+
assertType('0.5', rangeBound(0.5));
162+
assertType('1.5', constantBound(1.5));
163+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,15 @@ public function testBug15714(): void
593593
55,
594594
'Type float is not always the same as T. It breaks the contract for some argument types, typically subtypes.',
595595
],
596+
[
597+
'Function Bug15714ReturnType\\absolute() should return T of float but returns float.',
598+
69,
599+
],
600+
[
601+
'Function Bug15714ReturnType\\absolute() should return T of float but returns float.',
602+
72,
603+
'Type float is not always the same as T. It breaks the contract for some argument types, typically subtypes.',
604+
],
596605
]);
597606
}
598607

‎tests/PHPStan/Rules/Functions/data/bug-15714.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,29 @@ function returnsFloat(float $t, float $other): float
5757

5858
return $t;
5959
}
60+
61+
/**
62+
* @template T of float
63+
* @param T $t
64+
* @return T
65+
*/
66+
function absolute(float $t): float
67+
{
68+
if ($t < 0.0) {
69+
return abs($t);
70+
}
71+
72+
return abs($t);
73+
}
74+
75+
/**
76+
* @template T of float
77+
* @param T $t
78+
* @return T
79+
*/
80+
function merged(float $t): float
81+
{
82+
$sign = $t >= 0.0 ? 'positive' : 'negative';
83+
84+
return $t;
85+
}

‎turbo-ext/src/TemplateFloatType.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ class TemplateFloatType
4040
return pt_template_type_create<TemplateFloatType>(pt_ce_template_float_type, scope, strategy, variance, name, bound, defaultType);
4141
}
4242

43+
/* $this->getBound()->toAbsoluteNumber() — the trait's getter is the slot
44+
* (the class is final), the bound's method goes through its class entry
45+
* (a FloatType, FloatRangeType, ConstantFloatType or another template);
46+
* UNDEF = pending exception */
47+
zv::Val toAbsoluteNumber() const
48+
{
49+
zval *bound = pt_template_type_bound(self, pt_ce_template_float_type);
50+
if (UNEXPECTED(bound == NULL)) return zv::Val();
51+
return pt_type_call(Z_OBJ_P(bound), PT_LC("toabsolutenumber"), 0, NULL);
52+
}
53+
4354
private:
4455
zend_object *self;
4556
};
@@ -69,6 +80,8 @@ PT_MINIT_REGISTRATION(pt_register_template_float_type)
6980
if (UNEXPECTED(!PT_THIS.construct(args.scope, args.strategy, args.variance, args.name, args.bound, args.defaultType))) RETURN_THROWS();
7081
});
7182

83+
cls.method<&TemplateFloatType::toAbsoluteNumber>(sigs::toAbsoluteNumber);
84+
7285
/* the traits the twin uses, after the class's own methods */
7386
ptdecl::TemplateFloatType::registerTraits(cls);
7487

‎turbo-ext/src/generated/TemplateFloatType.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ inline constexpr char strings[] =
5353
"PHPStan\\Type\\FloatType\0" /* 182 */
5454
"default\0" /* 205 */
5555
"PHPStan\\Type\\Type\0" /* 213 */
56-
"__construct"; /* 231 */
56+
"__construct\0" /* 231 */
57+
"\0" /* 243 */
58+
"toAbsoluteNumber"; /* 244 */
5759
inline constexpr reg::PackedArg args[] = {
5860
reg::packed(0, 0, 6), /* __construct $scope */
5961
reg::packed(45, 0, 66), /* __construct $templateTypeStrategy */
6062
reg::packed(108, 0, 129), /* __construct $templateTypeVariance */
6163
reg::packed(171, MAY_BE_STRING), /* __construct $name */
6264
reg::packed(176, 0, 182), /* __construct $bound */
6365
reg::packed(205, MAY_BE_NULL, 213), /* __construct $default */
66+
reg::packed(243, 0, 213), /* toAbsoluteNumber return */
6467
};
6568
using Sig = reg::Sig<strings, args>;
6669
} // namespace sigtab
6770

6871
/* the signatures of the methods the class declares itself (a used trait's are in the trait's header) */
6972
namespace sig {
7073
inline constexpr sigtab::Sig __construct = { { 231 /* __construct */, 6, 0, 6, reg::NoArg, ZEND_ACC_PUBLIC } };
74+
inline constexpr sigtab::Sig toAbsoluteNumber = { { 244 /* toAbsoluteNumber */, 0, 6, 0, 6, ZEND_ACC_PUBLIC } };
7175
} // namespace sig
7276

7377
} // namespace ptdecl::TemplateFloatType

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,6 +5499,10 @@ public function resolve(): \PHPStan\Type\Type
54995499
'int1' => new \PHPStan\Type\Constant\ConstantIntegerType(1),
55005500
'range' => \PHPStan\Type\IntegerRangeType::fromInterval(0, 10),
55015501
'float' => new \PHPStan\Type\FloatType(),
5502+
'floatRange' => \PHPStan\Type\FloatRangeType::fromInterval(0.0, 10.0),
5503+
'floatRangeNegative' => \PHPStan\Type\FloatRangeType::fromInterval(-10.0, -1.0),
5504+
'floatMinus1_5' => new \PHPStan\Type\Constant\ConstantFloatType(-1.5),
5505+
'nan' => new \PHPStan\Type\Constant\ConstantFloatType(NAN),
55025506
'bool' => new \PHPStan\Type\BooleanType(),
55035507
'true' => new \PHPStan\Type\Constant\ConstantBooleanType(true),
55045508
'string' => new \PHPStan\Type\StringType(),
@@ -5637,6 +5641,7 @@ public function resolve(): \PHPStan\Type\Type
56375641
$r["$name comparisons"] = [$view($subject->isGreaterThan($others['null'], $templatePhpVersion)), $view($subject->isGreaterThan($others['int'], $templatePhpVersion)), $view($subject->isGreaterThanOrEqual($others['null'], $templatePhpVersion)), $view($subject->isGreaterThanOrEqual($others['int'], $templatePhpVersion)), $view($subject->isSmallerThan($others['int'], $templatePhpVersion)), $view($subject->isSmallerThanOrEqual($others['int'], $templatePhpVersion)), $view($subject->looseCompare($others['int'], $templatePhpVersion))];
56385642
$r["$name predicates"] = [$view($subject->isNull()), $view($subject->isObject()), $view($subject->isArray()), $view($subject->isString()), $view($subject->isInteger()), $view($subject->isIterable()), $view($subject->isCallable()), $view($subject->isScalar()), $view($subject->isConstantValue()), $view($subject->isSuperTypeOf($subject)), $view($subject->getObjectClassNames())];
56395643
$r["$name conversions"] = [$view($subject->toBoolean()), $view($subject->toNumber()), $view($subject->toString()), $view($subject->toInteger()), $view($subject->toFloat()), $view($subject->toArray()), $view($subject->getIterableKeyType()), $view($subject->getIterableValueType()), $view($subject->getClassStringType())];
5644+
$r["$name toAbsoluteNumber"] = $view($subject->toAbsoluteNumber());
56405645
$r["$name array ops"] = [$view($subject->getKeysArray()), $view($subject->getValuesArray()), $view($subject->popArray()), $view($subject->flipArray()), $view($subject->setOffsetValueType(new \PHPStan\Type\Constant\ConstantStringType('b'), new \PHPStan\Type\IntegerType())), $view($subject->setOffsetValueType(null, new \PHPStan\Type\FloatType())), $view($subject->unsetOffset(new \PHPStan\Type\Constant\ConstantStringType('a'))), $view($subject->getOffsetValueType(new \PHPStan\Type\Constant\ConstantStringType('a')))];
56415646
if ($subject instanceof \PHPStan\Type\Generic\GenericObjectType) {
56425647
$r["$name changeVariances"] = $view($subject->changeVariances([\PHPStan\Type\Generic\TemplateTypeVariance::createCovariant(), \PHPStan\Type\Generic\TemplateTypeVariance::createCovariant()]));

0 commit comments

Comments
 (0)