Skip to content

Commit fe7fb82

Browse files
committed
gccrs: add drops at the end of function bodies
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>
1 parent cf048a3 commit fe7fb82

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

gcc/rust/backend/rust-compile-base.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "rust-abi.h"
2121
#include "rust-compile-stmt.h"
2222
#include "rust-compile-expr.h"
23+
#include "rust-compile-drop.h"
2324
#include "rust-compile-fnparam.h"
2425
#include "rust-compile-var-decl.h"
2526
#include "rust-compile-type.h"
@@ -634,6 +635,8 @@ HIRCompileBase::compile_function_body (tree fndecl,
634635
return_value = coercion_site (id, return_value, actual, expected,
635636
lvalue_locus, rvalue_locus);
636637

638+
CompileDrop::emit_current_scope_drop_calls (ctx);
639+
637640
tree return_stmt
638641
= Backend::return_statement (fndecl, return_value, locus);
639642
ctx->add_statement (return_stmt);
@@ -656,6 +659,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
656659
// errors should have occurred
657660
location_t locus = function_body.get_locus ();
658661
tree return_value = unit_expression (locus);
662+
CompileDrop::emit_current_scope_drop_calls (ctx);
659663
tree return_stmt
660664
= Backend::return_statement (fndecl, return_value, locus);
661665
ctx->add_statement (return_stmt);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// { dg-output "d\r*\n" }
2+
// { dg-additional-options "-w" }
3+
#![feature(no_core)]
4+
#![feature(lang_items)]
5+
#![no_core]
6+
7+
extern "C" {
8+
fn printf(s: *const i8, ...);
9+
}
10+
11+
#[lang = "sized"]
12+
pub trait Sized {}
13+
14+
#[lang = "drop"]
15+
pub trait Drop {
16+
fn drop(&mut self);
17+
}
18+
19+
struct Droppable;
20+
21+
impl Drop for Droppable {
22+
fn drop(&mut self) {
23+
let msg = "d\n\0" as *const str as *const i8;
24+
unsafe {
25+
printf(msg);
26+
}
27+
}
28+
}
29+
30+
fn f() {
31+
let _x = Droppable;
32+
}
33+
34+
fn main() -> i32 {
35+
f();
36+
0
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// { dg-output "d\r*\n" }
2+
// { dg-additional-options "-w" }
3+
#![feature(no_core)]
4+
#![feature(lang_items)]
5+
#![no_core]
6+
7+
extern "C" {
8+
fn printf(s: *const i8, ...);
9+
}
10+
11+
#[lang = "sized"]
12+
pub trait Sized {}
13+
14+
#[lang = "drop"]
15+
pub trait Drop {
16+
fn drop(&mut self);
17+
}
18+
19+
struct Droppable;
20+
21+
impl Drop for Droppable {
22+
fn drop(&mut self) {
23+
let msg = "d\n\0" as *const str as *const i8;
24+
unsafe {
25+
printf(msg);
26+
}
27+
}
28+
}
29+
30+
fn main() -> i32 {
31+
let _x = Droppable;
32+
0
33+
}

0 commit comments

Comments
 (0)