From 80676cf44fc3da2eebee6f2506cfd1352e6fffb4 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Mon, 23 Mar 2020 21:57:54 +1100 Subject: [PATCH] cgen: fix struct init 0 fields & tests --- vlib/v/gen/cgen.v | 4 ++-- vlib/v/gen/tests/1.c | 7 ++++--- vlib/v/gen/tests/2.c | 3 ++- vlib/v/gen/tests/3.c | 4 +++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 19c9198c55..f955c61539 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1574,7 +1574,7 @@ fn (g mut Gen) struct_init(it ast.StructInit) { inited_fields << field g.write('\t.$field_name = ') g.expr_with_cast(it.exprs[i], it.expr_types[i], it.expected_types[i]) - g.writeln(', ') + g.writeln(',') } // The rest of the fields are zeroed. if is_struct { @@ -1587,7 +1587,7 @@ fn (g mut Gen) struct_init(it ast.StructInit) { g.writeln('\t.$field_name = $zero,') // zer0') } } - if it.fields.len == 0 { + if it.fields.len == 0 && info.fields.len == 0 { g.write('0') } g.write('}') diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 12ff2a3ac9..dc161de6ef 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -64,8 +64,8 @@ int main(int argc, char** argv) { println(int_str(localmod__pub_int_const)); int g = ((int)(3.0)); byte* bytes = ((byte*)(0)); - User* user_ptr = (User*)memdup(&(User){ .age = 0, -0}, sizeof(User)); + User* user_ptr = (User*)memdup(&(User){ .age = 0, +}, sizeof(User)); return 0; } @@ -88,7 +88,8 @@ i < 10; i++) { }); array_User users = new_array_from_c_array(1, 1, sizeof(User), (User[]){ (User){ - 0}, + .age = 0, + }, }); bool b = (*(bool*)array_get(bools, 0)); array_string mystrings = new_array_from_c_array(2, 2, sizeof(string), (string[]){ diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index b915fdc22c..fec9445c79 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -30,7 +30,8 @@ void init_user() { User get_user() { User user = (User){ - 0}; + .name = tos3(""), + }; return user; } diff --git a/vlib/v/gen/tests/3.c b/vlib/v/gen/tests/3.c index 6a1de173be..85d47aca73 100644 --- a/vlib/v/gen/tests/3.c +++ b/vlib/v/gen/tests/3.c @@ -66,7 +66,9 @@ int main(int argc, char** argv) { _vinit(); os__args = os__init_os_args(argc, (byteptr*)argv); User user = (User){ -0}; + .age = 0, + .name = tos3(""), + }; user.age = 10; user.age++; user.name = tos3("bob");