-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
RFC: Marking return values as important (#[\NoDiscard]) #17599
Conversation
d7c712e
to
296b7a4
Compare
6f8d462
to
7a5a475
Compare
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.
There could be some edge-cases with the optimizer. For example:
if (foo());
Should or should this not error? It will not without opcache, but will with.
Should not warn, but the RFC explicitly acknowledges OPcache optimizations (which is part of the reason for the explicit |
8fb967e
to
bb2125c
Compare
812e8a5
to
101d0cf
Compare
c893718
to
ad4999b
Compare
ad4999b
to
9ccd300
Compare
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.
I don't see big problems.
Run-time performance should be the same (thanks to RETVAL specialization).
JIT code increase will affect only functions with NoDiscard
attribute.
Right?
For optimizer, we may introduce an additional option that ignores NoDiscard
attribute (disabled by default). We already have few similar "unsafe" bits in opcache.optimization_level
.
Thank you for the review.
Yes, this is my understanding. The tracing JIT should only emit additional code if both the attribute is set and if the result in unused. The function jit has the same code size and will just call a different helper, as suggested by Ilija in https://github.com/php/php-src/pull/17599/files#r1934186865.
I tried to optimize the implementation for the “no warning happens” case, since users are expected to either use the value or to suppress it with Thus I would defer this to a later point when we have more data. |
You are right. It's not a big problem if we avoid inlining of functions with |
IMO - this attribute can be easily replaced with proper programming, e.g. returning array Aside remark - (sorry for interferring, I am not C-programmer and neither understand PHP-core stuff): |
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.
LGTM otherwise.
} | ||
zend_update_property_ex(zend_ce_nodiscard, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value); | ||
|
||
/* The assignment might fail due to 'readonly'. */ |
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.
I find code like this noisy, this just checks the contract of RETURN_THROWS. I would prefer to just keep the comment and remove the check
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.
I'm not sure I follow. Do you mean “remove the check” (thus keeping the RETURN_THROWS()
)? This would be incorrect if the assignment doesn't fail.
Or do you mean “remove the entire block, including the RETURN_THROWS()
”? In that case the comment no longer makes sense.
In any case, this mirrors the constructor of Deprecated::__construct()
, which has 2 readonly properties and where the RETURN_THROWS()
is actually doing something, so I find it helpful for clarity. In production builds this should hopefully not result in any emitted assembly either.
see also #11293 (comment) and #11293 (comment).
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.
I meant the check + the return, i.e. the entire block. I think the comment may still make sense to remind the reader that the property update can fail (but then the commit should probably placed a few lines up to just prior to the call).
058597e
to
609fcf5
Compare
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.
Ok, see nit
RFC: https://wiki.php.net/rfc/marking_return_value_as_important