From: Lishin <[email protected]>

Add the missing drop calls before returning from unit tail expressions.

gcc/rust/ChangeLog:

        * backend/rust-compile-base.cc
        (HIRCompileBase::compile_function_body): Emit current scope drops
        for unit tail expressions.

gcc/testsuite/ChangeLog:

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

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/1337bc73082ac4fcd5d7e6a2a9c276e12a7a63ac

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

 gcc/rust/backend/rust-compile-base.cc         |  2 +
 .../execute/drop-function-scope-unit-tail.rs  | 46 +++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 gcc/testsuite/rust/execute/drop-function-scope-unit-tail.rs

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index e049e18a4..4e618243b 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -739,6 +739,8 @@ HIRCompileBase::compile_function_body (tree fndecl,
          // just add the stmt expression
          ctx->add_statement (return_value);
 
+         CompileDrop::emit_current_scope_drop_calls (ctx);
+
          // now just return unit expression
          tree unit_expr = unit_expression (locus);
          tree return_stmt
diff --git a/gcc/testsuite/rust/execute/drop-function-scope-unit-tail.rs 
b/gcc/testsuite/rust/execute/drop-function-scope-unit-tail.rs
new file mode 100644
index 000000000..ab5bf3fb3
--- /dev/null
+++ b/gcc/testsuite/rust/execute/drop-function-scope-unit-tail.rs
@@ -0,0 +1,46 @@
+// { dg-output "d\r*\nd\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 foo() {}
+
+fn unit_tail_call() {
+    let _x = Droppable;
+    foo()
+}
+
+fn unit_tail_literal() {
+    let _x = Droppable;
+    ()
+}
+
+fn main() -> i32 {
+    unit_tail_call();
+    unit_tail_literal();
+    0
+}
\ No newline at end of file

base-commit: 99edb212c204385d994c72b85e8098be6f464226
-- 
2.54.0

Reply via email to