cgen: fix struct init 0 fields & tests

pull/4107/head
Joe Conigliaro 2020-03-23 21:57:54 +11:00
parent e13bbd8c40
commit 80676cf44f
4 changed files with 11 additions and 7 deletions

View File

@ -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('}')

View File

@ -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[]){

View File

@ -30,7 +30,8 @@ void init_user() {
User get_user() {
User user = (User){
0};
.name = tos3(""),
};
return user;
}

View File

@ -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");