[gcc r17-3090] gccrs: Emit function-scope drops for unit tail expressions

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:8b20ce82d7c7113d5ca8c6d198c8ed674f4b51d4

commit r17-3090-g8b20ce82d7c7113d5ca8c6d198c8ed674f4b51d4
Author: Lishin <[email protected]>
Date:   Fri Jun 19 18:14:41 2026 +0000

    gccrs: Emit function-scope drops for unit tail expressions
    
    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]>

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

diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index e049e18a4604..4e618243b3b3 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 000000000000..ab5bf3fb3e19
--- /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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.