gccrs: add drops at the end of function bodies

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>
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 54b5311..9678ad4 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -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 @@
 	  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 @@
       // 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
index 0000000..e4dd91f
--- /dev/null
+++ b/gcc/testsuite/rust/execute/drop-function-scope-unit.rs
@@ -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
index 0000000..7034486
--- /dev/null
+++ b/gcc/testsuite/rust/execute/drop-function-scope.rs
@@ -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
+}