Skip to content

GH-16067: prevent invalid abstract during compilation of methods #16069

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 5 commits into from
Oct 23, 2024

Conversation

DanielEScherzer
Copy link
Member

@DanielEScherzer DanielEScherzer commented Sep 25, 2024

For classes that are not declared abstract, produce a compiler error for any abstract methods. For anonymous classes and enums, since they cannot be made abstract, the error messages are slightly different.

@DanielEScherzer
Copy link
Member Author

DanielEScherzer commented Sep 25, 2024

Closes #16067, CC @iluuu1994

@iluuu1994
Copy link
Member

iluuu1994 commented Sep 25, 2024

This is not really the right approach. Instead, what you should do is simply check in zend_begin_method_decl() whether the method has the abstract flag, and whether ce is an anonymous class. No parser changes should be necessary. But as I mentioned in #15995 (review), it might be best to print this message for all non-abstract classes.

@DanielEScherzer DanielEScherzer changed the title GH-16067: prevent abstract in methods of anonymous classes GH-16067: prevent invalid abstract during compilation of methods Sep 26, 2024
@DanielEScherzer
Copy link
Member Author

ABI break should be removed, no longer changes any header files

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.

Thanks. The implementation now look more like I expected it to.

// anonymous and can't be abstract
const char *msg;
if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
msg = "Anonymous class %s cannot contain abstract method %s::%s";
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure there's a point in printing the class name twice? I don't have a strong opinion on this. Maybe @kocsismate does?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure there is a point other, but I couldn't really figure out a better way to do it - the message is about the class not being allowed to have abstract methods, and then the method name is usually given with the class name too. What would you think of ...abstract method ::foo without the class name?

Copy link
Member

Choose a reason for hiding this comment

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

E.g. "Must not declare abstract method Foo::bar() in anonymous class" would be an option. But it's not a big deal.

@DanielEScherzer
Copy link
Member Author

<rebased, GitHub was saying there was a merge conflict but git rebase master fixed it automatically>

DanielEScherzer and others added 5 commits October 23, 2024 11:24
So that it is clear what changes
For classes that are not declared `abstract`, produce a compiler error for any
`abstract` methods. For anonymous classes, since they cannot be made abstract,
the error message is slightly different.
@iluuu1994 iluuu1994 force-pushed the compiler-anon-abstract branch from 64bc13c to 8dbb886 Compare October 23, 2024 09:24
@iluuu1994 iluuu1994 merged commit 0b94cf6 into php:master Oct 23, 2024
9 of 10 checks passed
@iluuu1994
Copy link
Member

Thank you @DanielEScherzer!

@DanielEScherzer DanielEScherzer deleted the compiler-anon-abstract branch December 10, 2024 12:05
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