GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||
GeezerVison is showing github.com — [RFC] Ensure correct magic methods' signatures when typed by carusogabriel · Pull Request #4177 · php/php-src Conversation
|
|
What about |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
@nikic I ensured no return type to match the current implementation for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Another point is that I couldn't figure out how to implement the verification of the return of a magic method, the same was I tried this one with diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index c539b4ec10..bf0d9258fc 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6265,6 +6265,14 @@ zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */ } } + if (ce->__isset) { + if (UNEXPECTED(Z_TYPE_P(ce->__isset) != IS_BOOL)) { + zend_error_noreturn(E_COMPILE_ERROR, + "Method %s::%s() must return a boolean value", + ZSTR_VAL(ce->name), ZSTR_VAL(ce->__isset->common.function_name)); + } + } + if (implements_ast) { zend_compile_implements(implements_ast); } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
I'd probably rather confront special-casing of these methods, since returning from (as well as calling) constructor, destructor or clone is perfectly valid in current PHP: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Should then be something like: diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index c539b4ec10..bf0d9258fc 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6265,6 +6265,14 @@ zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */ } } + if (ce->__isset && ce->__isset->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { + if (UNEXPECTED(Z_TYPE_P(ce->__isset) != IS_BOOL)) { + zend_error_noreturn(E_COMPILE_ERROR, + "Method %s::%s() must return a boolean value", + ZSTR_VAL(ce->name), ZSTR_VAL(ce->__isset->common.function_name)); + } + } + if (implements_ast) { zend_compile_implements(implements_ast); } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Ok, I got the point of the BC you're trying to say: calling these methods themselves aren't gonna work anymore. So yes, probably after the implementation itself, we'll need a formal RFC. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
This is wrong, ce->__isset is a zend_function* not a zval* ... in zend_begin_method_decl is where magic methods are verified to conform (non-static, public). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@krakjoe Thanks for the help. I’ll implement the type check for the other methods so we can start the discussion on Internals about moving this RFC forward or not. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@carusogabriel I think my preference would be if magic methods could either omit the return type or have the correct one. For example |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@kocsismate I still have the same opinion: #4177 (comment) :) But this is something that I'll also raise on internals@/RFC |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
We've already introduced that kind of magic with the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
RFC now updated allowing |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oops, I wrote my comment yesterday, but forgot to submit it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Something that I need help to refactor: currently, there are some places with magic methods checks/rules:
Shouldn't we centralize it in one place, and if so, where? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@carusogabriel I think, first the correct behaviour which is in line with the RFC should be achieved and tested in more detail, because unfortunately I see some concerning things with the implementation :( Most of them have already been mentioned in previous review comments, but I'll collect them here:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Normal inheritance checks still apply to magic methods, so this should "just work". What is added here are only additional constraints because the methods are not part of an interface. Of course, it would be good to add one test to double check this.
As this RFC does not magically add any types (only checks those declared), I'm okay without additional tests for this. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@kocsismate About your comment here #4177 (review), this is a problem with all the methods, not just the magic ones. This won't be solved in this RFC :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@carusogabriel This is not a problem at all, since any type declaration must be enforced at runtime, there should just be no other option IMO. But based on chat messages and what the RFC said, my impression was that it's not the outcome you expect. E.g.:
That's why I suggested that you should add tests to double-check the run-time behaviour so that there are no surprises. Additionally, I think the RFC text should be updated if the patch is not in line with it, but the behaviour described in the RFC is fundamentally problematic. Or am I missing something? Sorry if I didn't get what the idea exactly is. However I think it's useful anyway to clarify it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@kocsismate Ok, let me try to make myself clearer. This RFC aims to expand existent checks for the magic method's signatures, and only that. These checks happen at "compile-time", like the ones that already exist: Runtime checks, to check what is been returning from the methods, they are in the "Future Scope" of this RFC. Existent checks for illustration: Is that clearer? I can update the RFC if there's something unclear there, just let me know :) Thanks for your concerns, btw. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@carusogabriel Thanks! Then the text of the RFC text as well as my understanding about your intentions is up-to-date :) However, I became a bit confused because of the example you posted (https://3v4l.org/gB9nf), since type declarations here are ensured at run-time, and I was in the belief that you explicitly don't want this. Additionally, it seemed to me during testing your patch locally that type declarations are also enforced at run-time. As I don't know how type verification of magic methods exactly work when there are type declarations, I think adding tests would be useful (although I saw there is a custom code for checking the return type of e.g. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
This PR will be rebased based on the changes made by @nikic (dcaf62f...efce369) fixing the problems with 5 different methods checking magic methods in different places :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Small reminder to rebase this :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just dropping by to say that I look forward to seeing this land ❤️ |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@jrfnl Almost there, I hope to get it ready by this weekend 😄 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@nikic @kocsismate Implementation updated. Please, take a look again :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
The implementation looks much better now! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Merged as e3d06fc. Thanks everyone for your reviews and help 🚀 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@carusogabriel why |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.