From: lishin <[email protected]>
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 <[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/e57e14b5092813a6f0ce405f47a7d880367ac4bd
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/4564
gcc/rust/backend/rust-compile-base.cc | 4 ++
.../rust/execute/drop-function-scope-unit.rs | 37 +++++++++++++++++++
.../rust/execute/drop-function-scope.rs | 33 +++++++++++++++++
3 files changed, 74 insertions(+)
create mode 100644 gcc/testsuite/rust/execute/drop-function-scope-unit.rs
create mode 100644 gcc/testsuite/rust/execute/drop-function-scope.rs
diff --git a/gcc/rust/backend/rust-compile-base.cc
b/gcc/rust/backend/rust-compile-base.cc
index 54b531173..9678ad482 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 @@ 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
index 000000000..e4dd91f98
--- /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 000000000..70344865d
--- /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
+}
--
2.54.0