forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test that demonstrates an incorrect return value when calling i…
…nto rust with non-c-like-enums.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/test/run-make-fulldeps/arguments-non-c-like-enum/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
18 changes: 18 additions & 0 deletions
18
src/test/run-make-fulldeps/arguments-non-c-like-enum/nonclike.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/run-make-fulldeps/arguments-non-c-like-enum/test.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdint.h> | ||
#include <assert.h> | ||
|
||
#include <stdio.h> | ||
|
||
/* 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; | ||
} |