-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
[WIP] Implement nullsafe ?-> operator #5619
Conversation
Would be nice to have null safe operator in PHP 8, is there a possibility that it will make the deadline before feature freeze? Though it wont hurt to have it in PHP 8.1 if it wont make the cut. |
I'll try 🙂 There are actually quite a few details to this that I didn't anticipate. We need to make sure to catch all the edge cases. |
4b04e3b
to
56ccf03
Compare
Its looking great so far, hope the RFC will be up and ready for voting before the feature freeze date of PHP 8. Best regards. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH the short circuiting implementation left me completely confused.
@nikic I haven't cleaned up all of the code yet. The naming is confusing (e.g. scope -> chain, short_circuiting -> ???). I'll try to improve that. The main idea is: Compiling an expression will check if a new short-circuiting chain needs to be created. This is the case if:
There are some annoyances like, not all expressions are compiled using |
f5785fd
to
f56c6ad
Compare
942da68
to
941785b
Compare
@nikic Is the implementation (especially Zend/zend_compile.c) less confusing to you now? |
c3feaa0
to
7f40ae7
Compare
@iluuu1994 What is the advantage/use of nullsafe operator in write context? |
|
@iluuu1994 I see, we check if object is not null, not the property (which will not make any sense)... |
PHP 8 already enters alpha, seems that feature freeze will be soon. Do you think your RFC is ready for voting phase before the feature freeze? This one I mean: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only (known) remaining issue: Using ?->
in an argument should emit a SEND_VAL
instead of SEND_REF
because otherwise the reference check is omitted and no error is printed when a null
value is passed.
Not sure I agree here, I think SEND_REF is right. This is similar to the write case, where |
Right. I mentioned that but wasn't sure if that was preferable. It's kind of different in that it won't just skip the call but pass It would be simpler to implement though (as it doesn't require special handling for |
Would it be possible to implement the short-circuiting by having a single stack of jmp_null opnums and then remembering (via return value) the current stack depth on entry into an expression, and popping the stack down to the remembered depth on exit? |
7f40ae7
to
4f6999c
Compare
017a213
to
18c69af
Compare
We should still treat nullsafe as a variable, just not a writable variable.
Presumably not intentional?
I don't believe this is needed anymore under the new semantics.
This is fine either way, but let's reduce the diff.
Forgot to git add this.
I think I'm happy with this now. @iluuu1994 Apart from the JIT support (which imho should be added separately), anything more you want to do here? |
@nikic A few tests are missing (see unresolved discussions) but apart from that no, I think this is ready 🙂 |
PHP 8 introduces a new object chaining operator `?->` which short-circuits moving to the next expression if the left-hand side evaluates to `null`. This operator can not be used in write-context, but that is not the concern of this sniff. Refs: * https://wiki.php.net/rfc/nullsafe_operator * php/php-src#5619 * php/php-src@9bf1198 Includes unit test.
PHP 8 introduces a new object chaining operator `?->` which short-circuits moving to the next expression if the left-hand side evaluates to `null`. This operator can not be used in write-context, but that is not the concern of this sniff. Refs: * https://wiki.php.net/rfc/nullsafe_operator * php/php-src#5619 * php/php-src@9bf1198 Includes unit test.
https://wiki.php.net/rfc/nullsafe_operator