GNOME Bugzilla – Bug 604371
Broken declaration and usage of constant multi-dimensional arrays
Last modified: 2018-02-21 08:53:20 UTC
This Vala code: const uint8[,] a = {{255,255}}; translates into the following C code: static const guint8 a[] = {{(guint8) 255, (guint8) 255}}; which is not understood by the C compiler (1-D array with 2-D initializer). It would be great if it was translated like this instead: static const guint8 a[] = {(guint8) 255, (guint8) 255}; static const gint a_length0 = 1; static const gint a_length1 = 2;
Tested on 0.8.1 with same results. Please fix it
Created attachment 178159 [details] [review] Added valid support for const multi-dimensional arrays The current vala implementation didn't allow to create C-valid multi-dimensional const arrays, cause no valid size was specified when more than one dimension was declared. I.e, this code: const int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {6,7,8}, {9}}; would have been transformed into: const gint numbers[5] = {{1, 2}, {3, 4}, {5, 6}, {6, 7, 8}, {9}}; instead of the valid: const gint numbers[5][3] = {{1, 2}, {3, 4}, {5, 6}, {6, 7, 8}, {9}}; My implementation fixes this. It also allows to get the size of the 2nd, 3rd, 5th (...) dimensions when they are specified; for example: const int[,,] tri_numbers = {{{1,2,3},{5,5,6}}, {{6,7}}}; now is transformed into: const gint tri_numbers[2][2][3] = {{{1, 2, 3}, {5, 5, 6}}, {{6, 7}}}; and you can correclty access to the length arrays: print("tri numbers sizes: %ldx%ldx%ld\n", tri_numbers.length[0], tri_numbers.length[1], tri_numbers.length[2]);
Created attachment 178162 [details] [review] vala array element access: use pointers when accessing to const multi-dimensional arrays The access to const multi-dimensional arrays must be fixed too. An array should be deferenced with pointers (as much as its dimension - 1), so this patch is needed too. I mean: const int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {6,7,8}, {9}}; print("Double Access %d, %d\n",numbers[1,1], numbers[3,2]); Should give this C output: g_print ("Double Access %d, %d\n", (*numbers)[(1 * G_N_ELEMENTS (numbers[0])) + 1], (*numbers)[(3 * G_N_ELEMENTS (numbers[0])) + 2]); I guess it would be better to simply output: g_print ("Double Access %d, %d\n", numbers[1][1],numbers[3][2]); But this would need a CCodeElementAccess rewrite, or adding a support for something like CCodeElementListAccess, to support more than one element access using the brackets.
Created attachment 179854 [details] [review] Added valid support for const multi-dimensional arrays Patch updated against latest master HEAD
Has this patch been applied ? I just downloaded vala 0.15.0, and the results seems to be the same. Additionally, I have a slighty different error either if I declare a const array[,] in a function (outside a class) or a static const array[,] (again, outside a class), where valac doesn't even generate a C file and says : --- ERROR:valaccodearraymodule.c:1183:vala_ccode_array_module_real_get_array_length_cvalue: assertion failed: (_tmp48_) Aborted ---
I'm using valac 0.25.3. When compiling this code: const int[][] a = {{1, 2}, {3, 4}}; valac outputs ** (valac:7287): CRITICAL **: vala_typesymbol_is_reference_type: assertion 'self != NULL' failed ** (valac:7287): CRITICAL **: vala_ccode_base_module_get_ccode_name: assertion 'node != NULL' failed and generated C code is: const (null) a[2] = {{1, 2}, {3, 4}}; which is obviously not compiling. Is it the same bug?
This code requires review in order to be merged. Hope this bug attention, can push it upstream.
Created attachment 368505 [details] [review] codegen: Use pointers when accessing constant multi-dimensional arrays
Created attachment 368506 [details] [review] codegen: Add valid support for const multi-dimensional arrays For example this const int[,,] tri_numbers = {{{1, 2, 3}, {5, 5, 6}}, {{6, 7}}}; correctly transfoms to const gint tri_numbers[2][2][3] = {{{1, 2, 3}, {5, 5, 6}}, {{6, 7}}};
*** Bug 735159 has been marked as a duplicate of this bug. ***
*** Bug 584370 has been marked as a duplicate of this bug. ***
Attachment 368505 [details] pushed as 9093a1d - codegen: Use pointers when accessing constant multi-dimensional arrays Attachment 368506 [details] pushed as dff67ff - codegen: Add valid support for const multi-dimensional arrays
*** Bug 628030 has been marked as a duplicate of this bug. ***