v: udpate elem_type in ArrayInit node & tests

pull/3944/head
Joe Conigliaro 2020-03-07 00:06:52 +11:00
parent fb785b8adf
commit 3de3b38dc1
4 changed files with 13 additions and 9 deletions

View File

@ -392,6 +392,9 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
}
// a = []
if array_init.exprs.len == 0 {
type_sym := c.table.get_type_symbol(c.expected_type)
array_info := type_sym.array_info()
array_init.elem_type = array_info.elem_type
return c.expected_type
}
// [1,2,3]
@ -412,6 +415,7 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
}
idx := c.table.find_or_register_array(elem_type, 1)
array_init.typ = table.new_type(idx)
array_init.elem_type = elem_type
}
// [50]byte
else if array_init.exprs.len == 1 && array_init.elem_type != table.void_type {
@ -423,7 +427,7 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
else {
c.error('expecting `int` for fixed size', array_init.pos)
}
}
}
idx := c.table.find_or_register_array_fixed(array_init.elem_type, fixed_size, 1)
array_type := table.new_type(idx)
array_init.typ = array_type

View File

@ -62,20 +62,20 @@ void foo(int a) {
for (int i = 0;
i < 10; i++) {
}
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (void[]){
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (int[]){
1, 2, 3,
});
array_int nums2 = array_slice(nums, 0, 2);
int number = nums[0];
array_bool bools = new_array_from_c_array(2, 2, sizeof(array_bool), (void[]){
array_bool bools = new_array_from_c_array(2, 2, sizeof(array_bool), (bool[]){
true, false,
});
array_User users = new_array_from_c_array(1, 1, sizeof(array_User), (void[]){
array_User users = new_array_from_c_array(1, 1, sizeof(array_User), (User[]){
(User){
},
});
bool b = bools[0];
array_string mystrings = new_array_from_c_array(2, 2, sizeof(array_string), (void[]){
array_string mystrings = new_array_from_c_array(2, 2, sizeof(array_string), (string[]){
tos3("a"), tos3("b"),
});
string s = mystrings[0];

View File

@ -66,7 +66,7 @@ void function2() {
}
void init_array() {
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (void[]){
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (int[]){
4, 2, 3,
});
}

View File

@ -41,13 +41,13 @@ int main() {
string e = tos3("hello");
e = testb(111);
e = tos3("world");
array_int f = new_array_from_c_array(4, 4, sizeof(array_int), (void[]){
array_int f = new_array_from_c_array(4, 4, sizeof(array_int), (int[]){
testa(), 2, 3, 4,
});
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), (void[]){
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), (string[]){
testb(1), tos3("hello"),
});
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), (void[]){
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), (Foo[]){
a,
});
Foo af_idx_el = arr_foo[0];