From f1b52b34f201456d8a0e9da907e3e5619fa24ac7 Mon Sep 17 00:00:00 2001 From: John VanEnk Date: Fri, 10 Jan 2020 17:42:30 -0800 Subject: [PATCH] Add a test that demonstrates an incorrect return value when calling into rust with non-c-like-enums. --- .../arguments-non-c-like-enum/Makefile | 7 ++++ .../arguments-non-c-like-enum/nonclike.rs | 18 +++++++++ .../arguments-non-c-like-enum/test.c | 40 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/test/run-make-fulldeps/arguments-non-c-like-enum/Makefile create mode 100644 src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs create mode 100644 src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c diff --git a/src/test/run-make-fulldeps/arguments-non-c-like-enum/Makefile b/src/test/run-make-fulldeps/arguments-non-c-like-enum/Makefile new file mode 100644 index 00000000000..5b5d620efe6 --- /dev/null +++ b/src/test/run-make-fulldeps/arguments-non-c-like-enum/Makefile @@ -0,0 +1,7 @@ +-include ../tools.mk + +all: + $(RUSTC) --crate-type=staticlib nonclike.rs + $(CC) test.c $(call STATICLIB,nonclike) $(call OUT_EXE,test) \ + $(EXTRACFLAGS) $(EXTRACXXFLAGS) + $(call RUN,test) 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 new file mode 100644 index 00000000000..563f907608b --- /dev/null +++ b/src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs @@ -0,0 +1,18 @@ +#![crate_type = "lib"] +#![crate_name = "nonclike"] + +#[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) { + (T::A(a), T::A(b)) => a + b, + (T::A(a), T::B) => a, + (T::B, T::A(b)) => b, + _ => 0, + } +} 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 new file mode 100644 index 00000000000..f622471e7d1 --- /dev/null +++ b/src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c @@ -0,0 +1,40 @@ +#include +#include + +#include + +/* This is the code generated by cbindgen 0.12.1 for the `enum T` type + * in nonclike.rs . */ +enum T_Tag { + A, + B, +}; +typedef uint8_t T_Tag; + +typedef struct { + uint64_t _0; +} A_Body; + +typedef struct { + T_Tag tag; + union { + A_Body a; + }; +} T; + +/* This symbol is defined by the Rust staticlib built from + * nonclike.rs. */ +extern uint64_t t_add(T a, T b); + +int main(int argc, char *argv[]) { + (void)argc; (void)argv; + + 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; +}