Hi

On 3/4/25 23:08, Alexandru Pătrănescu wrote:
Just asking, as my engine knowledge is a bit limited,
Wouldn't it be possible that when OPcache would optimize away the unused
variable assigned to a function, it could detect that that function have
the NoDiscard attribute and also remove the attribute effect (warning
triggering).

Not with how the implementation currently works: It's checking whether or not the FCALL opcode’s result is used or not. If the assignment is removed, the FCALL result will be unused.

Or identify that the function has the NoDiscard attribute and based on that
do not optimize away the variable?

First things first: It's not a super important optimization to have, the assignment to a variable is one of the cheaper operations - and given that OPcache needs to know about the function it's also pretty limited in application. Of course you can also always just remove the assignment yourself (or ask your favorite IDE or static analyzer to point it out for you).

That said: Checking for the attribute and performing the optimization is likely possible, though likely of limited cost/benefit. In any case that would be an implementation detail that does not change observable behavior. The RFC defines that `(void)` or alternatively `$_` will be the solution that guarantees that the warning will be suppressed (which will then also be part of the documentation), how exactly that will work internally is something that can and will change when refactoring and evolving the engine and OPcache.

Best regards
Tim Düsterhus

Reply via email to