Do not accept array as function arguments (#540)

This commit is contained in:
Emilio Cobos Álvarez 2020-07-14 19:15:00 +02:00 committed by GitHub
commit 9d7516ef1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 114 additions and 1 deletions

View File

@ -286,7 +286,14 @@ impl SynFnArgHelpers for syn::FnArg {
}) => match **pat {
syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
let ty = match Type::load(ty)? {
Some(x) => x,
Some(x) => {
if let Type::Array(_, _) = x {
return Err(
"Array as function arguments are not supported".to_owned()
);
}
x
}
None => return Ok(None),
};
Ok(Some((ident.to_string(), ty)))

View File

@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void pointer_test(const uint64_t *a);
void print_from_rust(void);

View File

@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void pointer_test(const uint64_t *a);
void print_from_rust(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View File

@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void pointer_test(const uint64_t *a);
void print_from_rust(void);

View File

@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void pointer_test(const uint64_t *a);
void print_from_rust(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View File

@ -0,0 +1,12 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
extern "C" {
void pointer_test(const uint64_t *a);
void print_from_rust();
} // extern "C"

View File

@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void pointer_test(const uint64_t *a);
void print_from_rust(void);

View File

@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void pointer_test(const uint64_t *a);
void print_from_rust(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View File

@ -0,0 +1,22 @@
#[no_mangle]
pub unsafe extern fn array_print(a: &[u64]) {
eprintln!("{:?}", a);
}
#[no_mangle]
pub unsafe extern fn array_test(a: [u64; 3]) {
array_print(&a);
}
#[no_mangle]
pub unsafe extern fn pointer_test(a: *const u64) {
let a = std::slice::from_raw_parts(a, 3);
array_print(a);
}
#[no_mangle]
pub unsafe extern fn print_from_rust() {
let a = [0, 1, 2];
array_print(&a);
}