[COMMITTED 32/77] gccrs: Emit function-scope drops for unit tail expressions

[email protected]
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.rust
Message-ID <[email protected]>
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]>
---
 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 e049e18a460..4e618243b3b 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 00000000000..ab5bf3fb3e1
--- /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
-- 
2.50.1
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.