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 inited_fields << field
g.write('\t.$field_name = ') g.write('\t.$field_name = ')
g.expr_with_cast(it.exprs[i], it.expr_types[i], it.expected_types[i]) 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. // The rest of the fields are zeroed.
if is_struct { if is_struct {
@ -1587,7 +1587,7 @@ fn (g mut Gen) struct_init(it ast.StructInit) {
g.writeln('\t.$field_name = $zero,') // zer0') 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('0')
} }
g.write('}') g.write('}')

View File

@ -65,7 +65,7 @@ int main(int argc, char** argv) {
int g = ((int)(3.0)); int g = ((int)(3.0));
byte* bytes = ((byte*)(0)); byte* bytes = ((byte*)(0));
User* user_ptr = (User*)memdup(&(User){ .age = 0, User* user_ptr = (User*)memdup(&(User){ .age = 0,
0}, sizeof(User)); }, sizeof(User));
return 0; return 0;
} }
@ -88,7 +88,8 @@ i < 10; i++) {
}); });
array_User users = new_array_from_c_array(1, 1, sizeof(User), (User[]){ array_User users = new_array_from_c_array(1, 1, sizeof(User), (User[]){
(User){ (User){
0}, .age = 0,
},
}); });
bool b = (*(bool*)array_get(bools, 0)); bool b = (*(bool*)array_get(bools, 0));
array_string mystrings = new_array_from_c_array(2, 2, sizeof(string), (string[]){ 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 get_user() {
User user = (User){ User user = (User){
0}; .name = tos3(""),
};
return user; return user;
} }

View File

@ -66,7 +66,9 @@ int main(int argc, char** argv) {
_vinit(); _vinit();
os__args = os__init_os_args(argc, (byteptr*)argv); os__args = os__init_os_args(argc, (byteptr*)argv);
User user = (User){ User user = (User){
0}; .age = 0,
.name = tos3(""),
};
user.age = 10; user.age = 10;
user.age++; user.age++;
user.name = tos3("bob"); user.name = tos3("bob");