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

param-out not taken into account when using generics #8891

Open
greg0ire opened this issue Dec 12, 2022 · 11 comments
Open

param-out not taken into account when using generics #8891

greg0ire opened this issue Dec 12, 2022 · 11 comments

Comments

@greg0ire
Copy link
Contributor

greg0ire commented Dec 12, 2022

Actual issue: #8891 (comment)

Psalm doesn't seem to realize that 42 is an integer here:

https://psalm.dev/r/654392acdb

It is suggesting to use param-out, which is confusing as I'm already using it.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/654392acdb
<?php

/**
 * @template T of positive-int
 */
class X {
    /**
     * @param-out T $s
     */
    function addFoo(mixed &$s) : void {
    }
}

function f3($z) {
    $x = new X();
    $x->addFoo($z);
    /** @psalm-trace $z */
}
Psalm output (using commit ef02ded):

INFO: Trace - 17:0 - $z: int<1, max>|mixed

INFO: MissingParamType - 14:13 - Parameter $z has no provided type

INFO: MissingReturnType - 14:10 - Method f3 does not have a return type, expecting void

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

The snippet seems outdated and does not illustrate the issue.

@greg0ire
Copy link
Contributor Author

You're right, I made a copy-paste mistake when examining another issue 😅 Thanks for pointing that out! Here is what I meant to post: https://psalm.dev/r/e46c3120a6

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T of int
 */
class X {
    /**
     * @param-out T $s
     */
    function addFoo(string &$s): void {
        $s = 42;
    }
}
Psalm output (using commit ef02ded):

ERROR: ReferenceConstraintViolation - 10:29 - Variable $s is limited to values of type T:X as int because it is passed by reference, 42 type found. Use @param-out to specify a different output type

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

While 42 is int, it's not necessarily T. E.g. when you have X<13>, T is 13, but you're outputting 42.

@greg0ire
Copy link
Contributor Author

greg0ire commented Dec 12, 2022

That makes sense I think, but then, does this also makes sense: https://psalm.dev/r/bb313a293d

There is no implementation here, so I think in this particular case Psalm should say nothing.

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T of int
 */
abstract class X {
    /**
     * @param-out T $s
     */
    abstract function addFoo(string &$s): void;
}
Psalm output (using commit ef02ded):

ERROR: ReferenceConstraintViolation - 10:38 - Variable $s is limited to values of type T:X as int because it is passed by reference, string type found. Use @param-out to specify a different output type

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

No, that doesn't make much sense to me (of course, it should be int &$s).

@greg0ire
Copy link
Contributor Author

greg0ire commented Dec 12, 2022

Should it? If the param enters the method as '42', and then is set to 42, then this should be correct, right (and is the purpose of param-out)?

Here is an example: https://github.com/doctrine/lexer/blob/2.0.x/tests/ConcreteLexer.php (not sure how realistic it is though, I known doctrine/orm doesn't do that kind of stuff).

@greg0ire
Copy link
Contributor Author

Here is an example with just Psalm: https://psalm.dev/r/b7de7cfb93
And that same code running: https://3v4l.org/jUfG8

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T of int
 */
abstract class X {
    /**
     * @param-out T $s
     */
    abstract function addFoo(string &$s): void;
}

/**
 * @extends X<int>
 */
class Y extends X
{
    public function addFoo(string &$s): void
    {
        $s = 42;
    }
}
Psalm output (using commit ef02ded):

ERROR: ReferenceConstraintViolation - 10:38 - Variable $s is limited to values of type T:X as int because it is passed by reference, string type found. Use @param-out to specify a different output type

ERROR: ReferenceConstraintViolation - 18:36 - Variable $s is limited to values of type string because it is passed by reference, 42 type found. Use @param-out to specify a different output type

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