Skip to content

GH-15994: fix suggestion that anonymous classes be made abstract #15995

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

Merged
merged 2 commits into from
Sep 25, 2024

Conversation

DanielEScherzer
Copy link
Member

In the process, remove the (incorrect) assumption that any abstract method that
needs to be implemented by a class that cannot itself be made abstract must be
a private method - the existing test for an enum already showed that this was
not the case.

@DanielEScherzer
Copy link
Member Author

The first commit is already sent at #15993, but to avoid merge conflicts I sent this as a follow-up to that patch - marking this as a draft until that merges

@DanielEScherzer DanielEScherzer marked this pull request as draft September 22, 2024 22:06
In the process, remove the (incorrect) assumption that any abstract method that
needs to be implemented by a class that cannot itself be made abstract must be
a private method - the existing test for an enum already showed that this was
not the case.
@DanielEScherzer DanielEScherzer marked this pull request as ready for review September 25, 2024 20:51
@DanielEScherzer
Copy link
Member Author

@iluuu1994 would you mind taking a look? This is a follow-up to #15993


$c = new class {
abstract public function f();
};
Copy link
Member

@iluuu1994 iluuu1994 Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this message doesn't make too much sense to me, as we cannot implement a method that is also declared as abstract. Maybe we should disallow abstract functions in anonymous classes in the compiler. This message would still make sense when the method comes from a parent class, interface or trait.

Granted, that is not really related to this change, so I'm happy if you move the abstract method to a parent class.

In reality, we should probably also change the error message for this:

class Foo {
    public abstract function bar();
}

Fatal error: Class Foo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Foo::bar)

Implementing Foo::bar() is a weird suggestion, given the abstract declaration comes from this class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should disallow abstract functions in anonymous classes in the compiler.

I can work on a patch for that

I'm happy if you move the abstract method to a parent class.

done

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I'll merge as soon as tests have passed 🙂

@iluuu1994 iluuu1994 merged commit b436ef4 into php:master Sep 25, 2024
10 checks passed
@iluuu1994
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants