Skip to content

self is incorrectly resolved when used as a return type in a trait method #18295

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

Open
xabbuh opened this issue Apr 10, 2025 · 3 comments · May be fixed by #18296
Open

self is incorrectly resolved when used as a return type in a trait method #18295

xabbuh opened this issue Apr 10, 2025 · 3 comments · May be fixed by #18296

Comments

@xabbuh
Copy link

xabbuh commented Apr 10, 2025

Description

The following code:

<?php

class CoreTransliterator
{
    public static function create(string $id, int $direction = self::FORWARD): ?CoreTransliterator
    {
    }
}

trait EmojiTransliteratorTrait
{
    public static function create(string $id, int $direction = self::FORWARD): self
    {
    }
}

class EmojiTransliterator extends CoreTransliterator
{
    use EmojiTransliteratorTrait;
}

Resulted in this output:

Fatal error: Declaration of EmojiTransliteratorTrait::create(string $id, int $direction = self::FORWARD): EmojiTransliteratorTrait must be compatible with CoreTransliterator::create(string $id, int $direction = self::FORWARD): ?CoreTransliterator

But I expected this output instead:

The code should continue to work. self in a trait should probably not be resolved at compile time.

PHP Version

8.5

Operating System

No response

Additional information

@iluuu1994
Copy link
Member

Probably caused by #15878, I'll have a look.

@Girgias
Copy link
Member

Girgias commented Apr 10, 2025

I did think that I had handled the trait case when doing the resolution to self but had messed it up.

If it does turn out to be that feel free to re-assign to me @iluuu1994

@iluuu1994
Copy link
Member

@Girgias I think this is caused by #15878. The issue is that we're only fixing trait scopes after parent inheritance. Even if self remains unresolved, fn->scope still refers to the trait, which makes the type check fail. The right thing is probably to fix the trait scope before parent inheritance, although this changes behavior for inheritance error messages. It also breaks two tests for edge-cases, which I need to look at.

iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 10, 2025
@iluuu1994 iluuu1994 linked a pull request Apr 10, 2025 that will close this issue
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants