1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

combining never/empty twice gives never (#5756)

This commit is contained in:
orklah 2021-05-13 00:46:37 +02:00 committed by GitHub
parent 40bc7cf294
commit ab1732de66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -332,6 +332,7 @@ class TypeCombiner
}
$has_empty = (int) isset($combination->value_types['empty']);
$has_never = false;
foreach ($combination->value_types as $type) {
if ($type instanceof TMixed
@ -344,18 +345,21 @@ class TypeCombiner
if (($type instanceof TEmpty || $type instanceof TNever)
&& (count($combination->value_types) > 1 || count($new_types))
) {
$has_never = true;
continue;
}
$new_types[] = $type;
}
if (!$new_types) {
if (!$new_types && !$has_never) {
throw new \UnexpectedValueException('There should be types here');
} elseif (!$new_types && $has_never) {
$union_type = Type::getNever();
} else {
$union_type = new Union($new_types);
}
$union_type = new Union($new_types);
if ($from_docblock) {
$union_type->from_docblock = true;
}

View File

@ -2,6 +2,7 @@
namespace Psalm;
use Psalm\Plugin\EventHandler\Event\StringInterpreterEvent;
use Psalm\Type\Atomic\TNever;
use function array_merge;
use function array_pop;
use function array_shift;
@ -301,6 +302,13 @@ abstract class Type
return new Union([$type]);
}
public static function getNever(): Union
{
$type = new TNever();
return new Union([$type]);
}
public static function getBool(): Union
{
$type = new TBool;

View File

@ -55,6 +55,15 @@ class TypeCombinationTest extends TestCase
return $type_name === "array";
}',
],
'NeverTwice' => [
'<?php
/** @return no-return */
function other() {
throw new Exception();
}
rand(0,1) ? die() : other();',
],
];
}