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

Incorrect template type inferred from closure #2575

Closed
muglug opened this issue Nov 10, 2019 · 3 comments
Closed

Incorrect template type inferred from closure #2575

muglug opened this issue Nov 10, 2019 · 3 comments
Labels
Milestone

Comments

@muglug
Copy link
Contributor

muglug commented Nov 10, 2019

Bug report

In the code below the type T should not be inferred from the callable param, but from the first arg. Callable params act as an upper bound on the possible type.

Code snippet that reproduces the problem

class C {
	public function foo() : void {}
}

/**
 * @psalm-template T
 * @psalm-param T $t
 * @psalm-param callable(?T):void $callable
 * @return T
 */
function makeConcrete($t, callable $callable) {
	$callable(rand(0, 1) ? $t : null);
	return $t;
}

$c = makeConcrete(new C(), function (?C $c) : void {});
$c->foo();

https://phpstan.org/r/a30bb64f-9723-460b-8d09-33efb3afdc40

Expected output

No issue

Actual output

Cannot call method foo() on C|null

@ondrejmirtes ondrejmirtes added this to the Generics milestone Dec 30, 2019
@phpstan-bot
Copy link
Contributor

@muglug After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-18: Cannot call method foo() on C|null.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@a1b7b38

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants