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

Closure params inappropriately bound #3158

Closed
muglug opened this issue Apr 10, 2020 · 3 comments
Closed

Closure params inappropriately bound #3158

muglug opened this issue Apr 10, 2020 · 3 comments
Labels
Milestone

Comments

@muglug
Copy link
Contributor

muglug commented Apr 10, 2020

Bug report

So, template upper & lower bounds. They're fun.

Here we have two types of bounds set: an upper bound (the T inside class-string<T>) and a lower bound (the T inside Closure(T):void).

What we care about is the upper bound – the most specific type we can infer in the call to createProxy, in this case A. It's incorrect to union the lower-bounded type and the upper-bounded type together, as they represent wholly different constraints on the value of T.

Code snippet that reproduces the problem

/**
 * @template T as object
 * @param class-string<T> $className
 * @param Closure(T):void $outmaker
 * @return T
 */
function createProxy(
    string $className,
    Closure $outmaker
) : object {
    $t = new $className();
    $outmaker($t);
    return $t;
}

class AParent {}

class A extends AParent {
    public function bar() : void {}
}

createProxy(A::class, function(AParent $o):void {})->bar();

Expected output

No issue

Actual

Call to an undefined method AParent::bar().

https://phpstan.org/r/a0d9b3fd-e762-4022-80b5-51223678a665

@phpstan-bot
Copy link
Contributor

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

@@ @@
-24: Call to an undefined method AParent::bar().
+No errors

@ondrejmirtes
Copy link
Member

I cheated a bit here (phpstan/phpstan-src@efa6aa6), I need to find the right solution.

@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 Jul 18, 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