]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: add drops at the end of function bodies
authorlishin <lishin1008@gmail.com>
Tue, 9 Jun 2026 16:04:55 +0000 (17:04 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 25 Jun 2026 17:21:39 +0000 (19:21 +0200)
Add function-scope Drop support for the implicit return case.
Add two test cases. This does not yet support explicit return.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::compile_function_body):
Add drop calls at the end of function bodies.

gcc/testsuite/ChangeLog:

* rust/execute/drop-function-scope-unit.rs: New test.
* rust/execute/drop-function-scope.rs: New test.

Signed-off-by: lishin <lishin1008@gmail.com>
gcc/rust/backend/rust-compile-base.cc
gcc/testsuite/rust/execute/drop-function-scope-unit.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/drop-function-scope.rs [new file with mode: 0644]

index 54b531173e94b831bbec68b7adc051a42e86b8ac..9678ad482d89548887066df6e5d550e4a28af08b 100644 (file)
@@ -20,6 +20,7 @@
 #include "rust-abi.h"
 #include "rust-compile-stmt.h"
 #include "rust-compile-expr.h"
+#include "rust-compile-drop.h"
 #include "rust-compile-fnparam.h"
 #include "rust-compile-var-decl.h"
 #include "rust-compile-type.h"
@@ -707,6 +708,8 @@ HIRCompileBase::compile_function_body (tree fndecl,
          return_value = coercion_site (id, return_value, actual, expected,
                                        lvalue_locus, rvalue_locus);
 
+         CompileDrop::emit_current_scope_drop_calls (ctx);
+
          tree return_stmt
            = Backend::return_statement (fndecl, return_value, locus);
          ctx->add_statement (return_stmt);
@@ -729,6 +732,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::emit_current_scope_drop_calls (ctx);
       tree return_stmt
        = Backend::return_statement (fndecl, return_value, locus);
       ctx->add_statement (return_stmt);
diff --git a/gcc/testsuite/rust/execute/drop-function-scope-unit.rs b/gcc/testsuite/rust/execute/drop-function-scope-unit.rs
new file mode 100644 (file)
index 0000000..e4dd91f
--- /dev/null
@@ -0,0 +1,37 @@
+// { dg-output "d\r*\n" }
+// { dg-additional-options "-w" }
+#![feature(no_core)]
+#![feature(lang_items)]
+#![no_core]
+
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "drop"]
+pub trait Drop {
+    fn drop(&mut self);
+}
+
+struct Droppable;
+
+impl Drop for Droppable {
+    fn drop(&mut self) {
+        let msg = "d\n\0" as *const str as *const i8;
+        unsafe {
+            printf(msg);
+        }
+    }
+}
+
+fn f() {
+    let _x = Droppable;
+}
+
+fn main() -> i32 {
+    f();
+    0
+}
diff --git a/gcc/testsuite/rust/execute/drop-function-scope.rs b/gcc/testsuite/rust/execute/drop-function-scope.rs
new file mode 100644 (file)
index 0000000..7034486
--- /dev/null
@@ -0,0 +1,33 @@
+// { dg-output "d\r*\n" }
+// { dg-additional-options "-w" }
+#![feature(no_core)]
+#![feature(lang_items)]
+#![no_core]
+
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "drop"]
+pub trait Drop {
+    fn drop(&mut self);
+}
+
+struct Droppable;
+
+impl Drop for Droppable {
+    fn drop(&mut self) {
+        let msg = "d\n\0" as *const str as *const i8;
+        unsafe {
+            printf(msg);
+        }
+    }
+}
+
+fn main() -> i32 {
+    let _x = Droppable;
+    0
+}