[gccrs COMMIT 1/3] gccrs: Add builder for current-scope drop cleanup

gerris.rs@gmail.com gerris.rs@gmail.com
Mon Jul 13 23:01:02 GMT 2026


From: Lishin <lishin1008@gmail.com>

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>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/6a983d07b20dac7014e21caaae865ed9504027a5

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4685

 gcc/rust/backend/rust-compile-drop.cc | 21 ++++++++++++++++++---
 gcc/rust/backend/rust-compile-drop.h  |  1 +
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-drop.cc b/gcc/rust/backend/rust-compile-drop.cc
index 5cb4db0e4..0062f30e2 100644
--- a/gcc/rust/backend/rust-compile-drop.cc
+++ b/gcc/rust/backend/rust-compile-drop.cc
@@ -90,9 +90,11 @@ CompileDrop::compile_drop_call (Bvariable *var, TyTy::BaseType *ty,
   return Backend::call_expression (fn_addr, {var_addr}, nullptr, locus);
 }
 
-void
-CompileDrop::emit_current_scope_drop_calls ()
+tree
+CompileDrop::build_current_scope_drop_cleanup ()
 {
+  std::vector<tree> drop_stmts;
+
   DropBuilder drop_builder (*ctx);
   auto &drop_candidates = drop_builder.peek_block_drop_candidates ();
 
@@ -109,8 +111,21 @@ CompileDrop::emit_current_scope_drop_calls ()
 
       tree drop_call = compile_drop_call (var, ty, it->locus);
       if (drop_call != NULL_TREE)
-	ctx->add_statement (convert_to_void (drop_call, ICV_STATEMENT));
+	drop_stmts.push_back (convert_to_void (drop_call, ICV_STATEMENT));
     }
+
+  if (drop_stmts.empty ())
+    return NULL_TREE;
+
+  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
diff --git a/gcc/rust/backend/rust-compile-drop.h b/gcc/rust/backend/rust-compile-drop.h
index 55a09c5dd..dea3e1935 100644
--- a/gcc/rust/backend/rust-compile-drop.h
+++ b/gcc/rust/backend/rust-compile-drop.h
@@ -31,6 +31,7 @@ public:
 
   bool type_has_drop_impl (TyTy::BaseType *ty);
 
+  tree build_current_scope_drop_cleanup ();
   void emit_current_scope_drop_calls ();
 
 private:

base-commit: a9a86ed4b0370586700fa2112bf5736ed50a24b8
-- 
2.54.0



More information about the Gcc-rust mailing list