Skip to content

Commit

Permalink
Add similar examples that work to each test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sw17ch authored and eddyb committed Feb 8, 2020
1 parent f1b52b3 commit 26bb0f1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
#![crate_type = "lib"]
#![crate_name = "nonclike"]

#[repr(C,u8)]
#[repr(C, u8)]
pub enum TT {
AA(u64, u64),
BB,
}

#[no_mangle]
pub extern "C" fn tt_add(a: TT, b: TT) -> u64 {
match (a, b) {
(TT::AA(a1, b1), TT::AA(a2, b2)) => a1 + a2 + b1 + b2,
(TT::AA(a1, b1), TT::BB) => a1 + b1,
(TT::BB, TT::AA(a1, b1)) => a1 + b1,
_ => 0,
}
}

#[repr(C, u8)]
pub enum T {
A(u64),
B,
}

#[no_mangle]
pub extern "C" fn t_add(a: T, b: T) -> u64 {
match (a,b) {
match (a, b) {
(T::A(a), T::A(b)) => a + b,
(T::A(a), T::B) => a,
(T::B, T::A(b)) => b,
Expand Down
32 changes: 29 additions & 3 deletions src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@

#include <stdio.h>

/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
* type in nonclike.rs . */
enum TT_Tag {
AA,
BB,
};
typedef uint8_t TT_Tag;

typedef struct {
uint64_t _0;
uint64_t _1;
} AA_Body;

typedef struct {
TT_Tag tag;
union {
AA_Body aa;
};
} TT;

/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
* in nonclike.rs . */
enum T_Tag {
Expand All @@ -22,18 +42,24 @@ typedef struct {
};
} T;

/* This symbol is defined by the Rust staticlib built from
/* These symbols are defined by the Rust staticlib built from
* nonclike.rs. */
extern uint64_t t_add(T a, T b);
extern uint64_t tt_add(TT a, TT b);

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

/* This example works. */
TT xx = { .tag = AA, .aa = { ._0 = 1, ._1 = 2 } };
TT yy = { .tag = AA, .aa = { ._0 = 10, ._1 = 20 } };
uint64_t rr = tt_add(xx, yy);
assert(33 == rr);

/* This one returns an incorrect result. */
T x = { .tag = A, .a = { ._0 = 1 } };
T y = { .tag = A, .a = { ._0 = 10 } };

uint64_t r = t_add(x, y);

assert(11 == r);

return 0;
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#![crate_type = "lib"]
#![crate_name = "nonclike"]

#[repr(C, u8)]
pub enum TT {
AA(u64, u64),
BB,
}

#[no_mangle]
pub extern "C" fn tt_new(a: u64, b: u64) -> TT {
TT::AA(a, b)
}

#[repr(C,u8)]
pub enum T {
A(u64),
Expand Down
30 changes: 29 additions & 1 deletion src/test/run-make-fulldeps/return-non-c-like-enum/test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
#include <stdint.h>
#include <assert.h>

/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
* type in nonclike.rs . */
enum TT_Tag {
AA,
BB,
};
typedef uint8_t TT_Tag;

typedef struct {
uint64_t _0;
uint64_t _1;
} AA_Body;

typedef struct {
TT_Tag tag;
union {
AA_Body aa;
};
} TT;

/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
* in nonclike.rs . */
enum T_Tag {
Expand All @@ -20,13 +40,21 @@ typedef struct {
};
} T;

/* This symbol is defined by the Rust staticlib built from
/* These symbols are defined by the Rust staticlib built from
* nonclike.rs. */
extern TT tt_new(uint64_t a, uint64_t b);
extern T t_new(uint64_t v);

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

/* This example works. */
TT tt = tt_new(10, 20);
assert(AA == tt.tag);
assert(10 == tt.aa._0);
assert(20 == tt.aa._1);

/* This one segfaults. */
T t = t_new(10);
assert(A == t.tag);
assert(10 == t.a._0);
Expand Down

0 comments on commit 26bb0f1

Please sign in to comment.