Skip to content

Commit f1b52b3

Browse files
sw17cheddyb
authored andcommitted
Add a test that demonstrates an incorrect return value when calling into rust with non-c-like-enums.
1 parent cd5ad99 commit f1b52b3

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --crate-type=staticlib nonclike.rs
5+
$(CC) test.c $(call STATICLIB,nonclike) $(call OUT_EXE,test) \
6+
$(EXTRACFLAGS) $(EXTRACXXFLAGS)
7+
$(call RUN,test)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_type = "lib"]
2+
#![crate_name = "nonclike"]
3+
4+
#[repr(C,u8)]
5+
pub enum T {
6+
A(u64),
7+
B,
8+
}
9+
10+
#[no_mangle]
11+
pub extern "C" fn t_add(a: T, b: T) -> u64 {
12+
match (a,b) {
13+
(T::A(a), T::A(b)) => a + b,
14+
(T::A(a), T::B) => a,
15+
(T::B, T::A(b)) => b,
16+
_ => 0,
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdint.h>
2+
#include <assert.h>
3+
4+
#include <stdio.h>
5+
6+
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
7+
* in nonclike.rs . */
8+
enum T_Tag {
9+
A,
10+
B,
11+
};
12+
typedef uint8_t T_Tag;
13+
14+
typedef struct {
15+
uint64_t _0;
16+
} A_Body;
17+
18+
typedef struct {
19+
T_Tag tag;
20+
union {
21+
A_Body a;
22+
};
23+
} T;
24+
25+
/* This symbol is defined by the Rust staticlib built from
26+
* nonclike.rs. */
27+
extern uint64_t t_add(T a, T b);
28+
29+
int main(int argc, char *argv[]) {
30+
(void)argc; (void)argv;
31+
32+
T x = { .tag = A, .a = { ._0 = 1 } };
33+
T y = { .tag = A, .a = { ._0 = 10 } };
34+
35+
uint64_t r = t_add(x, y);
36+
37+
assert(11 == r);
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)