1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #5924 from BafS/fix-type-error-patch-1

This commit is contained in:
Bruce Weirdan 2021-06-13 21:19:43 +03:00 committed by GitHub
commit 24d45a50de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 14 deletions

View File

@ -39,8 +39,6 @@ use function is_int;
use function preg_match; use function preg_match;
use function strtolower; use function strtolower;
use const PHP_INT_MAX;
/** /**
* @internal * @internal
*/ */
@ -273,6 +271,18 @@ class NonDivArithmeticOpAnalyzer
} }
} }
/**
* @param int|float $result
*/
private static function getNumericalType($result): Type\Union
{
if (is_int($result)) {
return Type::getInt(false, $result);
}
return Type::getFloat($result);
}
/** /**
* @param string[] &$invalid_left_messages * @param string[] &$invalid_left_messages
* @param string[] &$invalid_right_messages * @param string[] &$invalid_right_messages
@ -309,25 +319,19 @@ class NonDivArithmeticOpAnalyzer
$calculated_type = null; $calculated_type = null;
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus) { if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$calculated_type = Type::getInt(false, $left_type_part->value + $right_type_part->value); $result = $left_type_part->value + $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Minus) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
$calculated_type = Type::getInt(false, $left_type_part->value - $right_type_part->value); $result = $left_type_part->value - $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$calculated_type = Type::getInt(false, $left_type_part->value % $right_type_part->value); $calculated_type = Type::getInt(false, $left_type_part->value % $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mul) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
$result = $left_type_part->value * $right_type_part->value; $result = $left_type_part->value * $right_type_part->value;
if ($result <= PHP_INT_MAX) { $calculated_type = self::getNumericalType($result);
$calculated_type = Type::getInt(false, $result);
} else {
$calculated_type = Type::getFloat($result);
}
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Pow) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
$result = $left_type_part->value ** $right_type_part->value; $result = $left_type_part->value ** $right_type_part->value;
if ($result <= PHP_INT_MAX) { $calculated_type = self::getNumericalType($result);
$calculated_type = Type::getInt(false, $result);
} else {
$calculated_type = Type::getFloat($result);
}
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
$calculated_type = Type::getInt(false, $left_type_part->value | $right_type_part->value); $calculated_type = Type::getInt(false, $left_type_part->value | $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd) { } elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd) {

View File

@ -532,6 +532,29 @@ class BinaryOperationTest extends TestCase
'$a' => 'float' '$a' => 'float'
], ],
], ],
'IntOverflowPlus' => [
'<?php
$a = 2**62 - 1 + 2**62;
$b = 2**62 + 2**62 - 1; // plus results in a float',
'assertions' => [
'$a' => 'int',
'$b' => 'float',
],
],
'IntOverflowPowSub' => [
'<?php
$a = 2 ** 63;',
'assertions' => [
'$a' => 'float'
],
],
'IntOverflowSub' => [
'<?php
$a = (1 << 63) - (1 << 20);',
'assertions' => [
'$a' => 'float'
],
],
]; ];
} }