https://gcc.gnu.org/g:8bc32075bb2e59bc4915093c87e488ec30f2c2b3
commit r17-1895-g8bc32075bb2e59bc4915093c87e488ec30f2c2b3 Author: lishin <[email protected]> Date: Tue Jun 9 17:04:55 2026 +0100 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 <[email protected]> Diff: --- gcc/rust/backend/rust-compile-base.cc | 4 +++ .../rust/execute/drop-function-scope-unit.rs | 37 ++++++++++++++++++++++ gcc/testsuite/rust/execute/drop-function-scope.rs | 33 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 54b531173e94..9678ad482d89 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 000000000000..e4dd91f985af --- /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 000000000000..70344865dd0b --- /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 +}
