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

Bug with partially templated callable? #4531

Closed
thomasvargiu opened this issue Nov 10, 2020 · 3 comments
Closed

Bug with partially templated callable? #4531

thomasvargiu opened this issue Nov 10, 2020 · 3 comments

Comments

@thomasvargiu
Copy link
Contributor

Related to #4485

There is a problem when I don't explicity set a template for a callable return type.

https://psalm.dev/r/ec02d9ad54

If I ignore the return type template for the callable input, even if I'm not using it, checks fail.
If I put a template, it works as expected.

In this case I don't care about callable return type, so I shouldn't need to specify a template.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ec02d9ad54
<?php

/**
 * @template TP
 * @template TR
 * @psalm-param callable(TP): TR $func
 * @psalm-return callable(TP): TP
 */
function tee(callable $func): callable {
    return function ($a) use ($func) {
        $func($a);

        return $a;
    };
}

/**
 * @template TP
 * @psalm-param callable(TP): mixed $func
 * @psalm-return callable(TP): TP
 */
function tee2(callable $func): callable {
    return function ($a) use ($func) {
        $func($a);

        return $a;
    };
}

/** @psalm-trace $f */
$f = tee(fn (int $a): string => $a . 'foo');

/** @psalm-trace $f */
$f = tee2(fn (int $a): string => $a . 'foo');
Psalm output (using commit 5a5cbb2):

INFO: Trace - 31:1 - $f: callable(int):int

INFO: Trace - 34:1 - $f: callable(empty):empty

@thomasvargiu thomasvargiu changed the title Bug with partially templated callable Bug with partially templated callable? Nov 11, 2020
@muglug
Copy link
Collaborator

muglug commented Nov 11, 2020

There's no feasible way for Psalm to infer this type correctly, so it chooses the empty type (similar to a bottom type).

@muglug muglug closed this as completed Nov 11, 2020
@muglug
Copy link
Collaborator

muglug commented Nov 11, 2020

Correction: there is a way, and now it does.

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

No branches or pull requests

2 participants