-
Notifications
You must be signed in to change notification settings - Fork 8k
WIP: Zend: add native defer keyword (parser/compiler + VM opcodes) #20786
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
base: master
Are you sure you want to change the base?
Conversation
- Add T_DEFER token and ZEND_AST_DEFER AST node - Compile defer blocks by emitting a skip JMP + trailing JMP back - Add DEFER_PUSH/DEFER_RUN VM handlers using per-call defer_stack - Run defers before explicit/implicit returns (LIFO) - Add initial PHPT tests (basic/LIFO/early return/vars); exception unwind not yet supported
|
Hi @khaledalam. I'm skeptical of this approach. It effectively mirrors FWIU Zig executes Also note there are two recent, competing RFCs that are similar. https://wiki.php.net/rfc/optin_block_scoping |
|
https://packagist.org/packages/wikimedia/scoped-callback may solve what you are looking for, runs a function via a variable destructor when the variable goes out of scope |
Thanks @iluuu1994 totally fair points. I didn’t fully account for the optimizer complexity until I started implementing this and ran into test regressions, and I also underestimated the real complexity/semantics in loops (accumulating defers across iterations). |
Thanks @DanielEScherzer, good pointer. I actually built something very similar in userland (scoped-callback style). My motivation for exploring this in core is mainly language-level ergonomics and avoiding overhead in hot paths, but given the feedback here I'll re-evaluate the direction and how it overlaps with existing solutions/RFCs. |
Inspired by Zig's "defer", it schedules a block to run when the current function exits (return, exception, or natural end), executed in LIFO order, aimed at safer cleanup without repetitive try/finally scaffolding.