Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,10 @@ private static function reconcileFloat(
$old_var_type_string = $existing_var_type->getId();
$compared_with_one_value = self::isComparedWithOneValue($assertion);
$non_float_types = [];
$redundant = true;
// An integer calculation may have overflowed to a float.
$redundant = $is_equality
|| !$existing_var_type->from_calculation
|| !$existing_var_type->hasInt();

foreach ($existing_var_type->getAtomicTypes() as $type) {
if ($type instanceof TTemplateParam) {
Expand Down
40 changes: 40 additions & 0 deletions tests/IntRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,22 @@ function subtract(): int {
return $d;
}',
],
'negatedFloatCheckNarrowsPossiblyOverflowingAddition' => [
'code' => '<?php
/** @return positive-int */
function getPositive(): int { return 1; }

function f(): int {
$sum = getPositive() + getPositive();
if (!is_float($sum)) {
/** @psalm-check-type-exact $sum = int<2, max> */;
return $sum;
}

/** @psalm-check-type-exact $sum = float */;
return 0;
}',
],
'castOfAnAdditionWithAnIntRangeIsNotRedundant' => [
'code' => '<?php
function f(int $a, int $b): int {
Expand Down Expand Up @@ -1690,6 +1706,30 @@ function bar(int $_a, int $_b): void {}
public static function providerInvalidCodeParse(): iterable
{
return [
'floatCheckAfterExcludingOverflowIsImpossible' => [
'code' => '<?php
/** @return positive-int */
function getPositive(): int { return 1; }

function f(): void {
$sum = getPositive() + getPositive();
if (!is_float($sum)) {
if (is_float($sum)) {}
}
}',
'error_message' => 'TypeDoesNotContainType',
],
'negatedFloatCheckOnBoundedAdditionIsRedundant' => [
'code' => '<?php
/** @return int<1, 3> */
function getSmall(): int { return 1; }

function f(): void {
$sum = getSmall() + getSmall();
if (!is_float($sum)) {}
}',
'error_message' => 'RedundantCondition',
],
'additionOfBoundedRangesCannotOverflowToFloat' => [
'code' => '<?php
/** @return int<1, 3> */
Expand Down
Loading