From 26bb0f15e7e97bc93385296c2932193fe6da6300 Mon Sep 17 00:00:00 2001 From: John VanEnk Date: Fri, 10 Jan 2020 17:59:18 -0800 Subject: [PATCH] Add similar examples that work to each test. --- .../arguments-non-c-like-enum/nonclike.rs | 20 ++++++++++-- .../arguments-non-c-like-enum/test.c | 32 +++++++++++++++++-- .../return-non-c-like-enum/nonclike.rs | 11 +++++++ .../return-non-c-like-enum/test.c | 30 ++++++++++++++++- 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs b/src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs index 563f907608bd0..8f75076a30e99 100644 --- a/src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs +++ b/src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs @@ -1,7 +1,23 @@ #![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, @@ -9,7 +25,7 @@ pub enum T { #[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, diff --git a/src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c b/src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c index f622471e7d1fa..d34babcf3d33d 100644 --- a/src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c +++ b/src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c @@ -3,6 +3,26 @@ #include +/* 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 { @@ -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; diff --git a/src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs b/src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs index 36f618b05e843..700d2df1f3811 100644 --- a/src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs +++ b/src/test/run-make-fulldeps/return-non-c-like-enum/nonclike.rs @@ -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), diff --git a/src/test/run-make-fulldeps/return-non-c-like-enum/test.c b/src/test/run-make-fulldeps/return-non-c-like-enum/test.c index bcc17c0008d9c..3cbd8e6a20cb2 100644 --- a/src/test/run-make-fulldeps/return-non-c-like-enum/test.c +++ b/src/test/run-make-fulldeps/return-non-c-like-enum/test.c @@ -1,6 +1,26 @@ #include #include +/* 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 { @@ -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);