After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 604371 - Broken declaration and usage of constant multi-dimensional arrays
Broken declaration and usage of constant multi-dimensional arrays
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Arrays
0.7.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 584370 628030 735159 (view as bug list)
Depends on:
Blocks: 584370 735159
 
 
Reported: 2009-12-11 14:58 UTC by Ilya Mezhirov
Modified: 2018-02-21 08:53 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Added valid support for const multi-dimensional arrays (4.22 KB, patch)
2011-01-12 17:42 UTC, Marco Trevisan (Treviño)
none Details | Review
vala array element access: use pointers when accessing to const multi-dimensional arrays (1.14 KB, patch)
2011-01-12 17:47 UTC, Marco Trevisan (Treviño)
none Details | Review
Added valid support for const multi-dimensional arrays (4.23 KB, patch)
2011-02-02 01:02 UTC, Marco Trevisan (Treviño)
none Details | Review
codegen: Use pointers when accessing constant multi-dimensional arrays (1.16 KB, patch)
2018-02-17 21:41 UTC, Rico Tzschichholz
committed Details | Review
codegen: Add valid support for const multi-dimensional arrays (4.68 KB, patch)
2018-02-17 21:41 UTC, Rico Tzschichholz
committed Details | Review

Description Ilya Mezhirov 2009-12-11 14:58:12 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;
Comment 1 Aleksey Kunitskiy 2010-05-12 16:05:46 UTC
Tested on 0.8.1 with same results. Please fix it
Comment 2 Marco Trevisan (Treviño) 2011-01-12 17:42:30 UTC
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]);
Comment 3 Marco Trevisan (Treviño) 2011-01-12 17:47:00 UTC
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.
Comment 4 Marco Trevisan (Treviño) 2011-02-02 01:02:49 UTC
Created attachment 179854 [details] [review]
Added valid support for const multi-dimensional arrays

Patch updated against latest master HEAD
Comment 5 Elisabeth Henry 2011-12-09 02:32:10 UTC
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
---
Comment 6 Dmitry Golovin 2014-09-14 17:43:55 UTC
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?
Comment 7 Daniel Espinosa 2017-10-17 23:54:57 UTC
This code requires review in order to be merged. Hope this bug attention, can push it upstream.
Comment 8 Rico Tzschichholz 2018-02-17 21:41:02 UTC
Created attachment 368505 [details] [review]
codegen: Use pointers when accessing constant multi-dimensional arrays
Comment 9 Rico Tzschichholz 2018-02-17 21:41:15 UTC
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}}};
Comment 10 Rico Tzschichholz 2018-02-18 08:32:08 UTC
*** Bug 735159 has been marked as a duplicate of this bug. ***
Comment 11 Rico Tzschichholz 2018-02-18 08:32:19 UTC
*** Bug 584370 has been marked as a duplicate of this bug. ***
Comment 12 Rico Tzschichholz 2018-02-18 08:35:30 UTC
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
Comment 13 Michael 'Mickey' Lauer 2018-02-21 08:53:20 UTC
*** Bug 628030 has been marked as a duplicate of this bug. ***