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

Fix binding of T of static when called from a class scope #11370

Open
wants to merge 1 commit into
base: 6.x
Choose a base branch
from

Conversation

robchett
Copy link
Contributor

@robchett robchett commented Mar 20, 2025

Partial fix for #11368

Issue appears to cement itself here -

!$statements_analyzer->isStatic()
&& (!$method_id || $method_id->method_name !== '__construct')
? $context->self
: null,

as context->self = "User" but the method was called on an instance of UserId

After updating to

 !$statements_analyzer->isStatic()
    && (!$method_id || $method_id->method_name !== '__construct')
    ? ($method_id ? $method_id->fq_class_name : $context->self)
    : null,

https://psalm.dev/r/a32e05de27

ERROR: ArgumentTypeCoercion - 19:33 - Argument 1 of UserId1::equals expects User&static, but parent type UserId1 provided

becomes

ERROR: ArgumentTypeCoercion - 19:33 - Argument 1 of UserId1::equals expects UserId1&static, but parent type UserId1 provided

Now, using self instead of static works

    /**
     * @template T of self
     * @param T $uuid
     */
    final public function equals($uuid): void {}

I've seen the "A&static, but parent type A" style messaging elsewhere and unsure if this is now a legitimate issue or not.

Copy link

psalm-github-bot bot commented Mar 20, 2025

I found these snippets:

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

abstract class Uuid
{
    /**
     * @template T of static
     * @param T $uuid
     */
    final public function equals($uuid): void {}
}

class UserId1 extends Uuid {}
class UserId2 extends Uuid {}

class User
{
    public function equals(self $user): void
    {
        (new UserId1())->equals(new UserId2());
    }
}
Psalm output (using commit 514e954):

ERROR: ArgumentTypeCoercion - 19:33 - Argument 1 of UserId1::equals expects User&static, but parent type UserId2 provided

@robchett
Copy link
Contributor Author

Ignore the above snippet - I pasted the wrong link. https://psalm.dev/r/a32e05de27

Copy link

I found these snippets:

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

abstract class Uuid
{
    /**
     * @template T of static
     * @param T $uuid
     */
    final public function equals($uuid): void {}
}

class UserId1 extends Uuid {}
class UserId2 extends Uuid {}

class User
{
    public function equals(self $user): void
    {
        (new UserId1())->equals(new UserId2());
    }
}
Psalm output (using commit 514e954):

ERROR: ArgumentTypeCoercion - 19:33 - Argument 1 of UserId1::equals expects User&static, but parent type UserId2 provided

@robchett robchett force-pushed the static_template_binding_11368 branch from 8c1c8c3 to d3976ce Compare March 21, 2025 17:52
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

Successfully merging this pull request may close these issues.

1 participant