From 3de3b38dc1ed4ccaa27a83cd36f973aef06a3282 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Sat, 7 Mar 2020 00:06:52 +1100 Subject: [PATCH] v: udpate elem_type in ArrayInit node & tests --- vlib/v/checker/checker.v | 6 +++++- vlib/v/gen/tests/1.c | 8 ++++---- vlib/v/gen/tests/2.c | 2 +- vlib/v/gen/tests/4.c | 6 +++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8a4540cbb3..f747cf271f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index fb14a62d6f..5e4247c82c 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -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]; diff --git a/vlib/v/gen/tests/2.c b/vlib/v/gen/tests/2.c index 4730e848de..82fda87f93 100644 --- a/vlib/v/gen/tests/2.c +++ b/vlib/v/gen/tests/2.c @@ -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, }); } diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index ab60ae4d15..0c3fffefaf 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -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];