From: lishin Date: Tue, 9 Jun 2026 16:04:55 +0000 (+0100) Subject: gccrs: add drops at the end of function bodies X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8bc32075bb2e59bc4915093c87e488ec30f2c2b3;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 54b531173e9..9678ad482d8 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 00000000000..e4dd91f985a --- /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 00000000000..70344865dd0 --- /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 +}