https://gcc.gnu.org/g:35e344e5ccd0889840e3a7f164403cb36e525ce0
commit r17-3112-g35e344e5ccd0889840e3a7f164403cb36e525ce0 Author: Lishin <[email protected]> Date: Thu Jul 16 19:36:10 2026 +0000 gccrs: Apply try-finally cleanup for function-scope drops Remove the individual drop-emission calls, and centralize function-scope drop by building the cleanup and popping block. Apply the same cleanup path to constant-item and closure function bodies. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): Stop emitting drops directly in functions. (HIRCompileBase::compile_function): Use try-finally drop cleanup. (HIRCompileBase::compile_constant_item): Use try-finally drop cleanup. * backend/rust-compile-drop.cc (CompileDrop::emit_current_scope_drop_calls): Remove. * backend/rust-compile-drop.h: Remove the unused function declaration. * backend/rust-compile-expr.cc (CompileExpr::generate_closure_function): Use try-finally drop cleanup. Signed-off-by: Lishin <[email protected]> Diff: --- gcc/rust/backend/rust-compile-base.cc | 15 ++++++++------- gcc/rust/backend/rust-compile-drop.cc | 8 -------- gcc/rust/backend/rust-compile-drop.h | 1 - gcc/rust/backend/rust-compile-expr.cc | 5 ++++- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 04696787e407..7f9c56ca7b44 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -728,8 +728,6 @@ HIRCompileBase::compile_function_body (tree fndecl, return_value, locus); ctx->add_statement (assignment); - CompileDrop (ctx).emit_current_scope_drop_calls (); - result_reference = Backend::var_expression (fnctx.ret_addr, locus); tree return_stmt = Backend::return_statement (fndecl, result_reference, locus); @@ -740,8 +738,6 @@ HIRCompileBase::compile_function_body (tree fndecl, // just add the stmt expression ctx->add_statement (return_value); - CompileDrop (ctx).emit_current_scope_drop_calls (); - // now just return unit expression tree unit_expr = unit_expression (locus); tree return_stmt @@ -755,7 +751,7 @@ HIRCompileBase::compile_function_body (tree fndecl, // errors should have occurred location_t locus = function_body.get_locus (); tree return_value = unit_expression (locus); - CompileDrop (ctx).emit_current_scope_drop_calls (); + tree return_stmt = Backend::return_statement (fndecl, return_value, locus); ctx->add_statement (return_stmt); @@ -917,7 +913,10 @@ HIRCompileBase::compile_function ( ctx->push_fn (fndecl, return_address, tyret); compile_function_body (fndecl, *function_body, tyret); - tree bind_tree = ctx->pop_block (); + + tree cleanup = CompileDrop (ctx).build_current_scope_drop_cleanup (); + tree bind_tree + = ctx->pop_block_with_cleanup (cleanup, function_body->get_locus ()); gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); DECL_SAVED_TREE (fndecl) = bind_tree; @@ -1002,7 +1001,9 @@ HIRCompileBase::compile_constant_item ( ctx->add_statement (return_expr); } - tree bind_tree = ctx->pop_block (); + tree cleanup = CompileDrop (ctx).build_current_scope_drop_cleanup (); + tree bind_tree + = ctx->pop_block_with_cleanup (cleanup, const_value_expr.get_locus ()); gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); DECL_SAVED_TREE (fndecl) = bind_tree; diff --git a/gcc/rust/backend/rust-compile-drop.cc b/gcc/rust/backend/rust-compile-drop.cc index 0062f30e2611..ab63e9751146 100644 --- a/gcc/rust/backend/rust-compile-drop.cc +++ b/gcc/rust/backend/rust-compile-drop.cc @@ -120,13 +120,5 @@ CompileDrop::build_current_scope_drop_cleanup () return Backend::statement_list (drop_stmts); } -void -CompileDrop::emit_current_scope_drop_calls () -{ - tree cleanup = build_current_scope_drop_cleanup (); - if (cleanup != NULL_TREE) - ctx->add_statement (cleanup); -} - } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-drop.h b/gcc/rust/backend/rust-compile-drop.h index dea3e19359ba..f270356e1ee9 100644 --- a/gcc/rust/backend/rust-compile-drop.h +++ b/gcc/rust/backend/rust-compile-drop.h @@ -32,7 +32,6 @@ public: bool type_has_drop_impl (TyTy::BaseType *ty); tree build_current_scope_drop_cleanup (); - void emit_current_scope_drop_calls (); private: tree compile_drop_call (Bvariable *var, TyTy::BaseType *ty, location_t locus); diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 4bcd11da4166..52ddffd1df5e 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -24,6 +24,7 @@ #include "rust-compile-pattern.h" #include "rust-compile-resolve-path.h" #include "rust-compile-block.h" +#include "rust-compile-drop.h" #include "rust-compile-implitem.h" #include "rust-constexpr.h" #include "rust-compile-type.h" @@ -2907,7 +2908,9 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, ctx->add_statement (return_expr); } - tree bind_tree = ctx->pop_block (); + tree cleanup = CompileDrop (ctx).build_current_scope_drop_cleanup (); + tree bind_tree + = ctx->pop_block_with_cleanup (cleanup, function_body.get_locus ()); gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); DECL_SAVED_TREE (fndecl) = bind_tree;
