Skip to content

Commit feda320

Browse files
Lishin1215P-E-P
authored andcommitted
gccrs: Add execute tests for supported Drop behavior
Add execute coverage for Drop behavior that is already supported but was not covered by the testsuite. Cover function parameter drop order, explicit returns, ownership transfers through function arguments and return values, and branch-local values. gcc/testsuite/ChangeLog: * rust/execute/drop-explicit-return.rs: Test parameter drops on explicit return. * rust/execute/drop-function-boundary-moves.rs: New test. * rust/execute/drop-function-params.rs: Test multiple parameter drop order. * rust/execute/drop-if-else-local.rs: New test.
1 parent 9482951 commit feda320

4 files changed

Lines changed: 183 additions & 4 deletions

File tree

gcc/testsuite/rust/execute/drop-explicit-return.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-output "unit\r*\nmake_unit\r*\nunit_expr\r*\nmake_value\r*\nnonunit\r*\ninner\r*\nouter\r*\n" }
1+
// { dg-output "^unit\r*\nmake_unit\r*\nunit_expr\r*\nmake_value\r*\nnonunit\r*\ninner\r*\nouter\r*\nreturn local\r*\nreturn p2\r*\nreturn p1\r*\n$" }
22
// { dg-additional-options "-w" }
33
#![feature(no_core)]
44
#![feature(lang_items)]
@@ -21,6 +21,15 @@ struct UnitExprDroppable;
2121
struct NonUnitDroppable;
2222
struct OuterDroppable;
2323
struct InnerDroppable;
24+
struct ReturnFirstParam {
25+
value: i32,
26+
}
27+
struct ReturnSecondParam {
28+
value: i32,
29+
}
30+
struct ReturnLocal {
31+
value: i32,
32+
}
2433

2534
impl Drop for UnitDroppable {
2635
fn drop(&mut self) {
@@ -56,6 +65,27 @@ impl Drop for InnerDroppable {
5665
}
5766
}
5867

68+
impl Drop for ReturnFirstParam {
69+
fn drop(&mut self) {
70+
let msg = "return p1\n\0" as *const str as *const i8;
71+
unsafe { printf(msg); }
72+
}
73+
}
74+
75+
impl Drop for ReturnSecondParam {
76+
fn drop(&mut self) {
77+
let msg = "return p2\n\0" as *const str as *const i8;
78+
unsafe { printf(msg); }
79+
}
80+
}
81+
82+
impl Drop for ReturnLocal {
83+
fn drop(&mut self) {
84+
let msg = "return local\n\0" as *const str as *const i8;
85+
unsafe { printf(msg); }
86+
}
87+
}
88+
5989
fn make_unit () {
6090
let msg = "make_unit\n\0" as *const str as *const i8;
6191
unsafe { printf(msg); }
@@ -90,6 +120,11 @@ fn nested_return() {
90120
}
91121
}
92122

123+
fn parameter_return(_p1: ReturnFirstParam, _p2: ReturnSecondParam) {
124+
let _local = ReturnLocal { value: 3 };
125+
return;
126+
}
127+
93128
fn main() -> i32 {
94129
unit_return ();
95130
unit_return_expr ();
@@ -101,5 +136,10 @@ fn main() -> i32 {
101136

102137
nested_return();
103138

139+
parameter_return(
140+
ReturnFirstParam { value: 1 },
141+
ReturnSecondParam { value: 2 },
142+
);
143+
104144
0
105-
}
145+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// { dg-output "^drop 1\r*\ndrop 2\r*\ndrop 3\r*\n$" }
2+
// { dg-additional-options "-frust-borrowcheck -w" }
3+
4+
#![feature(no_core)]
5+
#![feature(lang_items)]
6+
#![no_core]
7+
8+
extern "C" {
9+
fn printf(s: *const i8, ...);
10+
}
11+
12+
#[lang = "sized"]
13+
pub trait Sized {}
14+
15+
#[lang = "drop"]
16+
pub trait Drop {
17+
fn drop(&mut self);
18+
}
19+
20+
struct Droppable {
21+
value: i32,
22+
}
23+
24+
impl Drop for Droppable {
25+
fn drop(&mut self) {
26+
let msg = "drop %d\n\0" as *const str as *const i8;
27+
unsafe {
28+
printf(msg, self.value);
29+
}
30+
}
31+
}
32+
33+
fn take(_value: Droppable) {}
34+
35+
fn move_parameter(value: Droppable) {
36+
let _moved = value;
37+
}
38+
39+
fn make() -> Droppable {
40+
let value = Droppable { value: 3 };
41+
value
42+
}
43+
44+
fn main() -> i32 {
45+
let value = Droppable { value: 1 };
46+
take(value);
47+
48+
move_parameter(Droppable { value: 2 });
49+
50+
let _returned = make();
51+
52+
0
53+
}

gcc/testsuite/rust/execute/drop-function-params.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-output "l\r*\np\r*\nl\r*\np\r*\n" }
1+
// { dg-output "^l\r*\np\r*\nl\r*\np\r*\nl\r*\np2\r*\np1\r*\n$" }
22
// { dg-additional-options "-w" }
33
#![feature(no_core)]
44
#![feature(lang_items)]
@@ -18,6 +18,12 @@ pub trait Drop {
1818

1919
struct ParamDroppable;
2020
struct LocalDroppable;
21+
struct FirstParamDroppable {
22+
value: i32,
23+
}
24+
struct SecondParamDroppable {
25+
value: i32,
26+
}
2127

2228
impl Drop for ParamDroppable {
2329
fn drop(&mut self) {
@@ -37,6 +43,24 @@ impl Drop for LocalDroppable {
3743
}
3844
}
3945

46+
impl Drop for FirstParamDroppable {
47+
fn drop(&mut self) {
48+
let msg = "p1\n\0" as *const str as *const i8;
49+
unsafe {
50+
printf(msg);
51+
}
52+
}
53+
}
54+
55+
impl Drop for SecondParamDroppable {
56+
fn drop(&mut self) {
57+
let msg = "p2\n\0" as *const str as *const i8;
58+
unsafe {
59+
printf(msg);
60+
}
61+
}
62+
}
63+
4064
fn named_param(_p: ParamDroppable) {
4165
let _l = LocalDroppable;
4266
}
@@ -45,8 +69,16 @@ fn wildcard_param(_: ParamDroppable) {
4569
let _l = LocalDroppable;
4670
}
4771

72+
fn multiple_params(_p1: FirstParamDroppable, _p2: SecondParamDroppable) {
73+
let _l = LocalDroppable;
74+
}
75+
4876
fn main() -> i32 {
4977
named_param(ParamDroppable);
5078
wildcard_param(ParamDroppable);
79+
multiple_params(
80+
FirstParamDroppable { value: 1 },
81+
SecondParamDroppable { value: 2 },
82+
);
5183
0
52-
}
84+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// { dg-output "^drop 1\r*\nafter\r*\ndrop 2\r*\nafter\r*\n$" }
2+
// { dg-additional-options "-w" }
3+
4+
#![feature(no_core)]
5+
#![feature(lang_items)]
6+
#![no_core]
7+
8+
extern "C" {
9+
fn printf(s: *const i8, ...);
10+
}
11+
12+
#[lang = "sized"]
13+
pub trait Sized {}
14+
15+
#[lang = "drop"]
16+
pub trait Drop {
17+
fn drop(&mut self);
18+
}
19+
20+
struct Droppable {
21+
value: i32,
22+
}
23+
24+
impl Drop for Droppable {
25+
fn drop(&mut self) {
26+
let msg = "drop %d\n\0" as *const str as *const i8;
27+
unsafe {
28+
printf(msg, self.value);
29+
}
30+
}
31+
}
32+
33+
fn droppable(value: i32) -> Droppable {
34+
Droppable { value }
35+
}
36+
37+
fn test(condition: bool) {
38+
if condition {
39+
let _value = droppable(1);
40+
} else {
41+
let _value = droppable(2);
42+
}
43+
44+
let msg = "after\n\0" as *const str as *const i8;
45+
unsafe {
46+
printf(msg);
47+
}
48+
}
49+
50+
fn main() -> i32 {
51+
test(true);
52+
test(false);
53+
0
54+
}

0 commit comments

Comments
 (0)