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

Contextual inference for Closure param types #7209

Closed
klimick opened this issue Dec 23, 2021 · 1 comment · Fixed by #7228
Closed

Contextual inference for Closure param types #7209

klimick opened this issue Dec 23, 2021 · 1 comment · Fixed by #7228

Comments

@klimick
Copy link
Contributor

klimick commented Dec 23, 2021

It's very painful that psalm can't infer closure parameters from context:
https://psalm.dev/r/310e6ed76e

Of course we can specify type hint. But if type have templates it will be lost:
https://psalm.dev/r/b60a08d22a

Doc blocks preserves templates, but code looks bad:
https://psalm.dev/r/7c9621b9d3

We could write code like this:

/** @var list<Foo<int>> */
$fooList = [];
$mapped = map($fooList, fn(Foo $foo) => [$foo]);

// $mapped is list<Foo<array{int}>>. No doc blocks!

From what I've learned, it is relatively easy to implement left-to-right args type resolving for furter closure params inference.
I don't think Psalm needs a right-to-left analysis that was be mentioned in #5820. From my point of view its rare and difficult case.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/310e6ed76e
<?php

/**
 * @template A
 * @template B
 * @param list<A> $collection
 * @param callable(A): B $ab
 * @return list<B>
 */
function map(array $collection, callable $ab): array
{
	$b = [];

    foreach ($collection as $a) {
    	$b[] = $ab($a);
    }

    return $b;
}

/** @var list<int> */
$numbers = [];

$mapped = map($numbers, fn($num) => $num + 1);
/** @psalm-trace $mapped */;
Psalm output (using commit 2827c3e):

INFO: MixedOperand - 24:37 - Left operand cannot be mixed

INFO: MissingClosureParamType - 24:28 - Parameter $num has no provided type

INFO: MissingClosureReturnType - 24:25 - Closure does not have a return type, expecting mixed

INFO: Trace - 25:28 - $mapped: list<mixed>
https://psalm.dev/r/b60a08d22a
<?php

/**
 * @template A
 * @template B
 * @param list<A> $collection
 * @param callable(A): B $ab
 * @return list<B>
 */
function map(array $collection, callable $ab): array
{
	$b = [];

    foreach ($collection as $a) {
    	$b[] = $ab($a);
    }

    return $b;
}

/** @template T */
final class Foo {}

/** @var list<Foo<int>> */
$fooList = [];

// Actual: list<array{Foo}>
// Expected: list<array{Foo<int>}>
$mapped = map($fooList, fn(Foo $num) => [$num]);
/** @psalm-trace $mapped */;
Psalm output (using commit 2827c3e):

INFO: Trace - 30:28 - $mapped: list<array{Foo}>
https://psalm.dev/r/7c9621b9d3
<?php

/**
 * @template A
 * @template B
 * @param list<A> $collection
 * @param callable(A): B $ab
 * @return list<B>
 */
function map(array $collection, callable $ab): array
{
	$b = [];

    foreach ($collection as $a) {
    	$b[] = $ab($a);
    }

    return $b;
}

/** @template T */
final class Foo {}

/** @var list<Foo<int>> */
$fooList = [];

//   Actual: list<array{Foo<int>}>
// Expected: list<array{Foo<int>}>
$mapped = map(
    $fooList,
    /** @param Foo<int> $num*/
    fn($num) => [$num],
);
/** @psalm-trace $mapped */;
Psalm output (using commit 2827c3e):

INFO: Trace - 34:28 - $mapped: list<array{Foo<int>}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant