Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inference high order function when closure param is omitted #10014

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ public static function analyze(

$inferred_arg_type = $statements_analyzer->node_data->getType($arg->value);

if (null !== $inferred_arg_type && null !== $template_result && null !== $param && null !== $param->type) {
if (null !== $inferred_arg_type
&& null !== $template_result
&& null !== $param
&& null !== $param->type
&& !$arg->unpack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was two failling tests here:

  • ArrayFunctionCallTest::testValidCode(splatArrayIntersect)
  • FunctionTemplateTest::testValidCode(splatTemplateParam)

And I've added && !$arg->unpack.
I don't know how to fill template result when arg is being unpacking.
This branch needs for this kind inference: https://psalm.dev/r/086e0799b6
I think we can leave it as is.

) {
$codebase = $statements_analyzer->getCodebase();

TemplateStandinTypeReplacer::fillTemplateResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ public static function analyze(
$function_call_info->function_id,
);

$template_result->lower_bounds += $already_inferred_lower_bounds;
$template_result->lower_bounds = array_merge(
$template_result->lower_bounds,
$already_inferred_lower_bounds,
);

if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
$stmt_type = FunctionCallReturnTypeFetcher::fetch(
Expand Down
42 changes: 42 additions & 0 deletions tests/CallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,48 @@ function appHandler3(int $param1, int $param2): array
'ignored_issues' => [],
'php_version' => '8.1',
],
'inferTypeWhenClosureParamIsOmitted' => [
'code' => '<?php
/**
* @template A
* @template B
* @param A $a
* @param callable(A): B $ab
* @return B
*/
function pipe(mixed $a, callable $ab): mixed
{
return $ab($a);
}
/**
* @template A
* @param callable(A): void $callback
* @return Closure(list<A>): list<A>
*/
function iterate(callable $callback): Closure
{
return function(array $list) use ($callback) {
foreach ($list as $item) {
$callback($item);
}
return $list;
};
}
$result1 = pipe(
[1, 2, 3],
iterate(fn($i) => print_r($i)),
);
$result2 = pipe(
[1, 2, 3],
iterate(fn() => print_r("noop")),
);',
'assertions' => [
'$result1===' => 'list<1|2|3>',
'$result2===' => 'list<1|2|3>',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
'varReturnType' => [
'code' => '<?php
$add_one = function(int $a) : int {
Expand Down