gccrs: Emit block scope drops through TRY_FINALLY_EXPR#4685
Open
Lishin1215 wants to merge 3 commits into
Open
Conversation
Lishin1215
commented
Jul 7, 2026
| if (body == NULL_TREE) | ||
| body = build_empty_stmt (cleanup_locus); | ||
|
|
||
| tree exceptional_cleanup = build_empty_stmt (cleanup_locus); |
Contributor
Author
There was a problem hiding this comment.
I followed the cleanup shape described in gccint 11.7.6, the TRY_FINALLY_EXPR
cleanup operand can be an EH_ELSE_EXPR selector.
I did not add the empty volatile asm used by rustc_codegen_gcc to keep the
EH_ELSE_EXPR around, because the normal cleanup side here contains the actual
Drop cleanup, and the relevant tests pass with an empty exceptional cleanup.
Add CompileDrop::build_current_scope_drop_cleanup to build the drop calls for the current scope as one statement tree. This allows a follow-up patch to place the same cleanup tree in a TRY_FINALLY_EXPR. gcc/rust/ChangeLog: * backend/rust-compile-drop.cc (CompileDrop::build_current_scope_drop_cleanup): New function to build current scope drop calls as a statement tree. (CompileDrop::emit_current_scope_drop_calls): Use it. * backend/rust-compile-drop.h (CompileDrop::build_current_scope_drop_cleanup): Declare. Signed-off-by: Lishin <lishin1008@gmail.com>
Add Context::pop_block_with_cleanup for blocks that need cleanup. Use it from CompileBlock so block-scope drops are emitted in a TRY_FINALLY_EXPR cleanup instead of at the end of the block body. gcc/rust/ChangeLog: * backend/rust-compile-block.cc (CompileBlock::visit): Build current scope drop cleanup and pass it when popping the block. * backend/rust-compile-context.h (Context::pop_block): Use pop_block_impl. (Context::pop_block_with_cleanup): New function for popping a block with cleanup code. (Context::pop_block_impl): New helper that adds the block statements directly, or wraps them in TRY_FINALLY_EXPR when cleanup is present. Signed-off-by: Lishin <lishin1008@gmail.com>
Use the Drop cleanup as the normal cleanup and an empty statement as the exceptional cleanup. This keeps block-scope Drop cleanup on normal control-flow exits without requiring the gccrs EH personality while stack unwinding is not implemented. gcc/rust/ChangeLog: * backend/rust-compile-context.h: (Context::pop_block_impl): Wrap Drop cleanup in an EH_ELSE_EXPR. Signed-off-by: Lishin <lishin1008@gmail.com>
0dad855 to
f470bb9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR consists of 3 commits.
It changes block-scope Drop emission to use a TRY_FINALLY_EXPR cleanup instead of
appending drop calls directly to the block body.
For example, a block like:
is lowered as:
This follows the same general cleanup/finally shape used by the Go frontend for defer handling.
For now, this PR only covers block-scope drops. Function-scope drops, explicit return cleanup,
break/continue are left for follow-up work.
commit 1
gccrs: Add helper to build scope drop cleanup
gcc/rust/ChangeLog:
commit 2
gccrs: Emit block scope drops through TRY_FINALLY_EXPR
gcc/rust/ChangeLog:
commit 3
gccrs: Use EH_ELSE_EXPR in block Drop TRY_FINALLY_EXPR
gcc/rust/ChangeLog:
Note:
TRY_FINALLY_EXPRcleanup originally causeddrop-nested-block-scope.rsto failto link with an undefined reference to
__gccrs_personality_v0.This version wraps the cleanup operand in
EH_ELSE_EXPR, using the Drop cleanupas the normal cleanup and an empty exceptional cleanup for now.
With that,
drop-nested-block-scope.rslinks and passes locally.