Skip to content

Commit

Permalink
Merge pull request #9466 from weirdan/unary-minus-overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Mar 7, 2023
2 parents c82191b + 193a0db commit 222887a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Psalm\Type\Union;
use RuntimeException;

use function is_int;

/**
* @internal
*/
Expand Down Expand Up @@ -51,7 +53,9 @@ public static function analyze(
continue;
}
if ($type_part instanceof TLiteralInt) {
$type_part = new TLiteralInt(-$type_part->value);
/** @var int|float $value */
$value = -$type_part->value;
$type_part = is_int($value) ? new TLiteralInt($value) : new TLiteralFloat($value);
} elseif ($type_part instanceof TLiteralFloat) {
$type_part = new TLiteralFloat(-$type_part->value);
} elseif ($type_part instanceof TIntRange) {
Expand Down
9 changes: 9 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,15 @@ function toPositiveInt(int $i): int
'$b===' => 'non-falsy-string',
],
],
'unaryMinusOverflows' => [
'code' => <<<'PHP'
<?php
$a = -(1 << 63);
PHP,
'assertions' => [
'$a===' => 'float(9.2233720368548E+18)',
],
],
];
}

Expand Down

0 comments on commit 222887a

Please sign in to comment.