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

callable(T): T doesn't preserve argument types on return types after invocation #10549

Closed
mpyw opened this issue Feb 8, 2024 · 5 comments
Closed

Comments

@mpyw
Copy link

mpyw commented Feb 8, 2024

Bug report

The following simple snippet works:

interface Iface {
}
class Impl implements Iface {}

/**
  * @template T of Iface
  * @param T $value
  * @return T
  */
function check($value) {
    return $value;
}

\phpstan\dumpType(new Impl);         // Dumped type: Impl
\phpstan\dumpType(check(new Impl));  // Dumped type: Impl <-- Correct

But the following doesn't work:

interface Iface {
}
class Impl implements Iface {}

/**
  * @template T of Iface
  * @param callable(T): T $func
  */
function check($func): void {
    \phpstan\dumpType(new Impl);        // Dumped type: Impl
    \phpstan\dumpType($func(new Impl)); // Dumped type: T of Iface (function check(), argument) <-- Wrong
                                        // Parameter #1 $ of callable callable(T): T expects T of Iface, Impl given.
}

Code snippet that reproduces the problem

https://phpstan.org/r/552156a3-7933-43b9-9d56-076043d96034

Expected output

// Dumped type: Impl
// Dumped type: Impl

Did PHPStan help you today? Did it make you happy in any way?

Everyday it helps our teams! Thank you

@mpyw
Copy link
Author

mpyw commented Feb 8, 2024

After the review, it looks to be not a bug, thanks

@mpyw mpyw closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
@mpyw mpyw reopened this Feb 8, 2024
@mpyw
Copy link
Author

mpyw commented Feb 8, 2024

But are there any nice workaround for this?

@mpyw
Copy link
Author

mpyw commented Feb 8, 2024

Possibly equivalent in TypeScript:

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAmPJyDeyBQzkwD2hAXMiAK4C2ARtDgL44IA2cAzu8gJJUAOLVPxYQqEcFzSIUmPMj4UaLYAgLFkAXmQBGANyMcOGBRAIwwQiGQIAFhAQBrABQwQZADwAVZBAAekEAATSXQIAD4nUAUwMk8ASk0w5HiyADdCYECsOQRLdjBUEGjNcggAdx5hfXwagHpa5AA9AH4cvILCCjBirVdIoq646prkeqbWpiA

interface iface { 
  foo: number
}
class Impl implements iface {
  public foo = 1;
}

function check(fn: <T extends iface>(input: T) => T): void {
  const input = new Impl;
     // ^? const input: Impl
  const output = fn(input);
     // ^? const output: Impl
}

The point of view is Generic parameters for callable

@ondrejmirtes
Copy link
Member

With this signature:

/**
  * @template T of Iface
  * @param callable(T): T $func
  */
function check($func): void {

You're making the function check generic, but you're not making the callable generic. Here's a feature request: #8964

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 Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants