From 75ff76a999412939a5be9f8f30722e38139d5d35 Mon Sep 17 00:00:00 2001 From: chengqinglin Date: Mon, 11 Apr 2022 11:14:55 +0800 Subject: [PATCH] Fix alloc empty struct array error --- vlib/arrays/arrays_test.v | 8 ++++++++ vlib/v/gen/c/cheaders.v | 8 ++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/vlib/arrays/arrays_test.v b/vlib/arrays/arrays_test.v index 74eb69b7d3..176d7c0088 100644 --- a/vlib/arrays/arrays_test.v +++ b/vlib/arrays/arrays_test.v @@ -300,3 +300,11 @@ fn test_alias_string_contains() { names := [Str('')] assert (Str('') in names) == true } + +struct XYZ {} + +fn test_array_append_empty_struct() { + mut names := []XYZ{cap: 2} + names << XYZ{} + assert (XYZ{} in names) == true +} diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index c7117e1c1e..16ed50e537 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -258,7 +258,7 @@ static void __closure_destroy(void *closure) { const c_common_macros = ' #define EMPTY_VARG_INITIALIZATION 0 -#define EMPTY_STRUCT_DECLARATION +#define EMPTY_STRUCT_DECLARATION char _dummy #define EMPTY_STRUCT_INITIALIZATION // Due to a tcc bug, the length of an array needs to be specified, but GCC crashes if it is... #define EMPTY_ARRAY_OF_ELEMS(x,n) (x[]) @@ -300,8 +300,6 @@ const c_common_macros = ' #endif #ifdef __TINYC__ - #undef EMPTY_STRUCT_DECLARATION - #define EMPTY_STRUCT_DECLARATION char _dummy #undef EMPTY_ARRAY_OF_ELEMS #define EMPTY_ARRAY_OF_ELEMS(x,n) (x[n]) #undef __NOINLINE @@ -576,10 +574,8 @@ voidptr memdup(voidptr src, int sz); #define _Atomic volatile // MSVC cannot parse some things properly - #undef EMPTY_STRUCT_DECLARATION #undef OPTION_CAST - #define EMPTY_STRUCT_DECLARATION char __pad #define OPTION_CAST(x) #undef __NOINLINE #undef __IRQHANDLER @@ -633,7 +629,7 @@ typedef uint8_t byte; typedef uint32_t rune; typedef size_t usize; typedef ptrdiff_t isize; -#ifndef VNOFLOAT +#ifndef VNOFLOAT typedef float f32; typedef double f64; #else