Skip to content

Commit 26bb0f1

Browse files
sw17cheddyb
authored andcommitted
Add similar examples that work to each test.
1 parent f1b52b3 commit 26bb0f1

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
#![crate_type = "lib"]
22
#![crate_name = "nonclike"]
33

4-
#[repr(C,u8)]
4+
#[repr(C, u8)]
5+
pub enum TT {
6+
AA(u64, u64),
7+
BB,
8+
}
9+
10+
#[no_mangle]
11+
pub extern "C" fn tt_add(a: TT, b: TT) -> u64 {
12+
match (a, b) {
13+
(TT::AA(a1, b1), TT::AA(a2, b2)) => a1 + a2 + b1 + b2,
14+
(TT::AA(a1, b1), TT::BB) => a1 + b1,
15+
(TT::BB, TT::AA(a1, b1)) => a1 + b1,
16+
_ => 0,
17+
}
18+
}
19+
20+
#[repr(C, u8)]
521
pub enum T {
622
A(u64),
723
B,
824
}
925

1026
#[no_mangle]
1127
pub extern "C" fn t_add(a: T, b: T) -> u64 {
12-
match (a,b) {
28+
match (a, b) {
1329
(T::A(a), T::A(b)) => a + b,
1430
(T::A(a), T::B) => a,
1531
(T::B, T::A(b)) => b,

src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33

44
#include <stdio.h>
55

6+
/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
7+
* type in nonclike.rs . */
8+
enum TT_Tag {
9+
AA,
10+
BB,
11+
};
12+
typedef uint8_t TT_Tag;
13+
14+
typedef struct {
15+
uint64_t _0;
16+
uint64_t _1;
17+
} AA_Body;
18+
19+
typedef struct {
20+
TT_Tag tag;
21+
union {
22+
AA_Body aa;
23+
};
24+
} TT;
25+
626
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
727
* in nonclike.rs . */
828
enum T_Tag {
@@ -22,18 +42,24 @@ typedef struct {
2242
};
2343
} T;
2444

25-
/* This symbol is defined by the Rust staticlib built from
45+
/* These symbols are defined by the Rust staticlib built from
2646
* nonclike.rs. */
2747
extern uint64_t t_add(T a, T b);
48+
extern uint64_t tt_add(TT a, TT b);
2849

2950
int main(int argc, char *argv[]) {
3051
(void)argc; (void)argv;
3152

53+
/* This example works. */
54+
TT xx = { .tag = AA, .aa = { ._0 = 1, ._1 = 2 } };
55+
TT yy = { .tag = AA, .aa = { ._0 = 10, ._1 = 20 } };
56+
uint64_t rr = tt_add(xx, yy);
57+
assert(33 == rr);
58+
59+
/* This one returns an incorrect result. */
3260
T x = { .tag = A, .a = { ._0 = 1 } };
3361
T y = { .tag = A, .a = { ._0 = 10 } };
34-
3562
uint64_t r = t_add(x, y);
36-
3763
assert(11 == r);
3864

3965
return 0;

src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#![crate_type = "lib"]
22
#![crate_name = "nonclike"]
33

4+
#[repr(C, u8)]
5+
pub enum TT {
6+
AA(u64, u64),
7+
BB,
8+
}
9+
10+
#[no_mangle]
11+
pub extern "C" fn tt_new(a: u64, b: u64) -> TT {
12+
TT::AA(a, b)
13+
}
14+
415
#[repr(C,u8)]
516
pub enum T {
617
A(u64),

src/test/run-make-fulldeps/return-non-c-like-enum/test.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
#include <stdint.h>
22
#include <assert.h>
33

4+
/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
5+
* type in nonclike.rs . */
6+
enum TT_Tag {
7+
AA,
8+
BB,
9+
};
10+
typedef uint8_t TT_Tag;
11+
12+
typedef struct {
13+
uint64_t _0;
14+
uint64_t _1;
15+
} AA_Body;
16+
17+
typedef struct {
18+
TT_Tag tag;
19+
union {
20+
AA_Body aa;
21+
};
22+
} TT;
23+
424
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
525
* in nonclike.rs . */
626
enum T_Tag {
@@ -20,13 +40,21 @@ typedef struct {
2040
};
2141
} T;
2242

23-
/* This symbol is defined by the Rust staticlib built from
43+
/* These symbols are defined by the Rust staticlib built from
2444
* nonclike.rs. */
45+
extern TT tt_new(uint64_t a, uint64_t b);
2546
extern T t_new(uint64_t v);
2647

2748
int main(int argc, char *argv[]) {
2849
(void)argc; (void)argv;
2950

51+
/* This example works. */
52+
TT tt = tt_new(10, 20);
53+
assert(AA == tt.tag);
54+
assert(10 == tt.aa._0);
55+
assert(20 == tt.aa._1);
56+
57+
/* This one segfaults. */
3058
T t = t_new(10);
3159
assert(A == t.tag);
3260
assert(10 == t.a._0);

0 commit comments

Comments
 (0)