From: Lishin <[email protected]>

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]>
---
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/f53262ab410a732629a2d2ea56ceffc9eb367637

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/4711

 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 04696787e..7f9c56ca7 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 0062f30e2..ab63e9751 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 dea3e1935..f270356e1 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 4bcd11da4..52ddffd1d 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;

base-commit: ab11f462825159529a085117f88c2214fc30e191
-- 
2.54.0

Reply via email to