Skip to content

Commit 121f182

Browse files
committed
Check only the assigned value itself for a float zero
Constant arrays holding a zero are no longer searched, so `$a = [-0.0]; $a = [0.0];` and `if ($a === [0.0]) { $a = [0.0]; }` are reported again. Object shapes and general arrays never have a single finite type and do not reach this check.
1 parent 0d60c67 commit 121f182

5 files changed

Lines changed: 14 additions & 76 deletions

File tree

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,31 +3093,16 @@ private static function redundant(ExpressionResult $rhs, Expr $target, Expressio
30933093
$nativeType = $nativeType->getOffsetValueType($nativeOffset);
30943094
}
30953095
$values = $type->getFiniteTypes();
3096-
if (count($values) !== 1 || !$values[0]->equals($rhs->getType()) || self::containsFloatZero($values[0])) {
3096+
if (count($values) !== 1 || !$values[0]->equals($rhs->getType())) {
3097+
return null;
3098+
}
3099+
// 0.0 and -0.0 are identical (`===`) but distinguishable (`(string)`, `fdiv()`),
3100+
// and `$f === 0.0` narrows $f to 0.0 while it may still hold -0.0
3101+
if (in_array(0.0, $values[0]->getConstantScalarValues(), true)) {
30973102
return null;
30983103
}
30993104
$nativeValues = $nativeType->getFiniteTypes();
31003105
return count($nativeValues) === 1 && $nativeValues[0]->equals($rhs->getNativeType()) ? $rhs->getType() : null;
31013106
}
31023107

3103-
/**
3104-
* 0.0 and -0.0 are identical (`===`) but distinguishable (`(string)`, `fdiv()`),
3105-
* and `$f === 0.0` narrows $f to 0.0 while it may still hold -0.0, so a type
3106-
* holding a float zero cannot prove that assigning it again changes nothing.
3107-
*/
3108-
private static function containsFloatZero(Type $type): bool
3109-
{
3110-
if (in_array(0.0, $type->getConstantScalarValues(), true)) {
3111-
return true;
3112-
}
3113-
foreach ($type->getConstantArrays() as $array) {
3114-
foreach ($array->getValueTypes() as $valueType) {
3115-
if (self::containsFloatZero($valueType)) {
3116-
return true;
3117-
}
3118-
}
3119-
}
3120-
return false;
3121-
}
3122-
31233108
}

‎tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testRedundantAssignmentOfSignedZero(): void
210210
{
211211
$this->analyse([__DIR__ . '/data/unused-variable-redundant-signed-zero.php'], [
212212
['Value assigned to variable $x is never read before being overwritten.', 12],
213-
['Variable $x is assigned value 1.5 but it already has that value.', 58],
213+
['Variable $x is assigned value 1.5 but it already has that value.', 50],
214214
]);
215215
}
216216

‎tests/PHPStan/Rules/DeadCode/data/unused-variable-redundant-signed-zero.php‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ function looseZeroNarrowing(float $f): void
3535
sink($f);
3636
}
3737

38-
function zeroInArray(): void
39-
{
40-
$a = [-0.0];
41-
sink($a);
42-
$a = [0.0];
43-
sink($a);
44-
}
45-
4638
function zeroAtOffset(): void
4739
{
4840
$a = ['k' => -0.0];
@@ -59,14 +51,6 @@ function nonZeroFloatIsStillRedundant(): void
5951
sink($x);
6052
}
6153

62-
function zeroInNestedArray(): void
63-
{
64-
$a = [[-0.0]];
65-
sink($a);
66-
$a = [[0.0]];
67-
sink($a);
68-
}
69-
7054
function zeroAtNestedOffset(): void
7155
{
7256
$a = ['x' => ['y' => -0.0]];

‎turbo-ext/src/AssignHandler.cpp‎

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,9 +4810,12 @@ class AssignHandler
48104810
bool equal;
48114811
AH_OK(ahEquals(zend_hash_index_find(Z_ARRVAL_P(values.raw()), 0), rhsType.raw(), equal));
48124812
if (!equal) return ahNull();
4813-
bool floatZero;
4814-
AH_OK(containsFloatZero(zend_hash_index_find(Z_ARRVAL_P(values.raw()), 0), floatZero));
4815-
if (floatZero) return ahNull();
4813+
// in_array(0.0, ..., true): matches -0.0 too, never NAN; the sign of a zero is observable
4814+
AH_VAL(scalarValues, ahTypeOp(zend_hash_index_find(Z_ARRVAL_P(values.raw()), 0), PT_OP_GET_CONSTANT_SCALAR_VALUES, 0, NULL));
4815+
for (auto entry : zv::TableRef(Z_ARRVAL_P(scalarValues.raw()))) {
4816+
zval *value = entry.value().deref().raw();
4817+
if (Z_TYPE_P(value) == IS_DOUBLE && Z_DVAL_P(value) == 0.0) return ahNull();
4818+
}
48164819
}
48174820
AH_VAL(nativeValues, ahCall(nativeType.raw(), PT_LC("getfinitetypes"), 0, NULL));
48184821
if (zend_hash_num_elements(Z_ARRVAL_P(nativeValues.raw())) != 1) return ahNull();
@@ -4822,35 +4825,6 @@ class AssignHandler
48224825
if (!equal) return ahNull();
48234826
return pt_expression_result_get_type(rhs);
48244827
}
4825-
4826-
/* (twin 3042); false = pending exception */
4827-
static bool containsFloatZero(zval *type, bool &result)
4828-
{
4829-
result = false;
4830-
{
4831-
AH_VALB(scalarValues, ahTypeOp(type, PT_OP_GET_CONSTANT_SCALAR_VALUES, 0, NULL));
4832-
for (auto entry : zv::TableRef(Z_ARRVAL_P(scalarValues.raw()))) {
4833-
zval *value = entry.value().deref().raw();
4834-
// in_array(0.0, ..., true): -0.0 is identical to 0.0, NAN to nothing
4835-
if (Z_TYPE_P(value) == IS_DOUBLE && Z_DVAL_P(value) == 0.0) {
4836-
result = true;
4837-
return true;
4838-
}
4839-
}
4840-
}
4841-
AH_VALB(constantArrays, ahTypeOp(type, PT_OP_GET_CONSTANT_ARRAYS, 0, NULL));
4842-
for (auto arrayEntry : zv::TableRef(Z_ARRVAL_P(constantArrays.raw()))) {
4843-
AH_VALB(valueTypes, ahTypeOp(arrayEntry.value().deref().raw(), PT_OP_GET_VALUE_TYPES, 0, NULL));
4844-
for (auto entry : zv::TableRef(Z_ARRVAL_P(valueTypes.raw()))) {
4845-
zval *valueType = entry.value().deref().raw();
4846-
bool ok = true;
4847-
pt_engine_with_stack([&]() { ok = containsFloatZero(valueType, result); });
4848-
AH_OKB(ok);
4849-
if (result) return true;
4850-
}
4851-
}
4852-
return true;
4853-
}
48544828
};
48554829

48564830
} // namespace phpstanturbo

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ inline constexpr char strings[] =
227227
"array\0" /* 3789 */
228228
"hasArrayReference\0" /* 3795 */
229229
"rhs\0" /* 3813 */
230-
"redundant\0" /* 3817 */
231-
"type\0" /* 3827 */
232-
"containsFloatZero"; /* 3832 */
230+
"redundant"; /* 3817 */
233231
inline constexpr reg::PackedArg args[] = {
234232
reg::packed(0, 0, 25), /* __construct $templateArgumentObserver */
235233
reg::packed(76, 0, 99), /* __construct $varAnnotationProcessor */
@@ -475,8 +473,6 @@ inline constexpr reg::PackedArg args[] = {
475473
reg::packed(1975, 0, 1376), /* redundant $target */
476474
reg::packed(1521, 0, 1529), /* redundant $storage */
477475
reg::packed(1396, MAY_BE_NULL, 2038), /* redundant return */
478-
reg::packed(3827, 0, 2038), /* containsFloatZero $type */
479-
reg::packed(1396, MAY_BE_BOOL), /* containsFloatZero return */
480476
};
481477
using Sig = reg::Sig<strings, args>;
482478
} // namespace sigtab
@@ -523,7 +519,6 @@ inline constexpr sigtab::Sig getLiteralArrayKeyType = { { 3654 /* getLiteralArra
523519
inline constexpr sigtab::Sig getOriginalPropertyType = { { 3765 /* getOriginalPropertyType */, 3, 234, 3, 237, ZEND_ACC_PRIVATE } };
524520
inline constexpr sigtab::Sig hasArrayReference = { { 3795 /* hasArrayReference */, 1, 238, 1, 239, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
525521
inline constexpr sigtab::Sig redundant = { { 3817 /* redundant */, 3, 240, 3, 243, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
526-
inline constexpr sigtab::Sig containsFloatZero = { { 3832 /* containsFloatZero */, 1, 244, 1, 245, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
527522
} // namespace sig
528523

529524
} // namespace ptdecl::AssignHandler

0 commit comments

Comments
 (0)