Skip to content

Commit

Permalink
Merge pull request #10254 from tuqqu/never-function-return-error-message
Browse files Browse the repository at this point in the history
Fix error message for implicitly returning functions with `never` return type
  • Loading branch information
orklah authored Oct 9, 2023
2 parents cad10a1 + 2bc3309 commit 3f7306d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public static function verifyReturnType(
)
)
&& !$return_type->isVoid()
&& !$return_type->isNever()
&& !$inferred_yield_types
&& (!$function_like_storage || !$function_like_storage->has_yield)
&& $function_returns_implicitly
Expand All @@ -222,7 +223,7 @@ public static function verifyReturnType(
) {
if (IssueBuffer::accepts(
new InvalidReturnType(
$cased_method_id . ' is not expected to return any values but it does, '
$cased_method_id . ' is not expected to return, but it does, '
. 'either implicitly or explicitly',
$return_type_location,
),
Expand Down
50 changes: 50 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,26 @@ function aggregate($type) {
return $t;
}',
],
'neverReturnType' => [
'code' => '<?php
function exitProgram(bool $die): never
{
if ($die) {
die;
}
exit;
}
function throwError(): never
{
throw new Exception();
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}

Expand Down Expand Up @@ -1807,6 +1827,36 @@ public function process(): mixed
'ignored_issues' => [],
'php_version' => '8.0',
],
'implicitReturnFromFunctionWithNeverReturnType' => [
'code' => <<<'PHP'
<?php
function foo(): never
{
if (rand(0, 1)) {
exit();
}
}
PHP,
'error_message' => 'InvalidReturnType',
'ignored_issues' => [],
'php_version' => '8.1',
],
'implicitReturnFromFunctionWithNeverReturnType2' => [
'code' => <<<'PHP'
<?php
function foo(bool $x): never
{
while (true) {
if ($x) {
break;
}
}
}
PHP,
'error_message' => 'InvalidReturnType',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}

0 comments on commit 3f7306d

Please sign in to comment.