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 529866 - no way to specify the type of the array length variable
no way to specify the type of the array length variable
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Arrays
0.3.x
Other All
: High normal
: 1.0
Assigned To: Vala maintainers
Vala maintainers
: 588104 601257 (view as bug list)
Depends on:
Blocks: 582092 594376
 
 
Reported: 2008-04-25 10:45 UTC by Daniel Svensson
Modified: 2010-04-27 09:03 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Arrays: Add array_length_type annotation to methods (4.22 KB, patch)
2009-08-12 21:18 UTC, Mark Lee
none Details | Review
updated based on juergbi's comments on IRC (4.40 KB, patch)
2009-08-13 20:35 UTC, Mark Lee
committed Details | Review
Support array_length_type for fields. (4.66 KB, patch)
2010-02-18 09:42 UTC, Evan Nemerson
committed Details | Review
support array_length_type for method arguments (3.67 KB, patch)
2010-04-03 05:46 UTC, Evan Nemerson
none Details | Review

Description Daniel Svensson 2008-04-25 10:45:08 UTC
Please describe the problem:
Vala assumes the length variable should be a signed integer:

my_c_function(char **foo, int length);

but some methods may look like this:

my_c_function(char **foo, usigned int length);

This causes a GCC warning about differing in signedness. Perhaps some .vapi metadata stuff to tell the specific type would be in order.

Steps to reproduce:


Actual results:


Expected results:


Does this happen every time?


Other information:
Comment 1 Daniel Svensson 2008-04-25 10:47:07 UTC
An example of this is g_key_file_get_string_list:
http://library.gnome.org/devel/glib/stable/glib-Key-value-file-parser.html#g-key-file-get-string-list

That uses gsize for the length argument instead of int.
Comment 2 Jürg Billeter 2008-04-25 12:18:06 UTC
Confirming, that's especially important for out parameters.
Comment 3 Daniel Svensson 2008-05-01 16:01:59 UTC
A first hacky attempt to fix this bug:

http://df3e55588a1c8d51.paste.se/

The patch is just the first attempt that solved it, and is nowhere near usable, but perhaps points to the direction where it should be fixed.

It does only handle this case:

myfunction(out foo); that translates to for example this:

(_tmp2 = (xmmsc_result_get_bin (res, &_tmp1, &_tmp0), data_length1 = &_tmp0), (data = (g_free (data), NULL)), data = _tmp1, _tmp2)

Where _tmp1 is the array argument and _tmp0 is the unsigned integer length argument.

The .vapi syntax in the above generated example is:

public bool get_bin ([CCode(array_length_type="uint")] out uchar[] val);

It should ofc handle cases where you pass _out_ the array, and also the case where an array is returned.

Anyways, this is a bit over my head I think.. it took me a couple of hours a day for a whole week to get this working, so maybe someone who knows the vala compiler better should have a look at this bug.
Comment 4 Daniel Svensson 2008-05-02 08:25:53 UTC
Oh.. and in the above comment:

(_tmp2 = (xmmsc_result_get_bin (res, &_tmp1, &_tmp0), data_length1 = &_tmp0),
(data = (g_free (data), NULL)), data = _tmp1, _tmp2)

Should ofc be:

(_tmp2 = (xmmsc_result_get_bin (res, &_tmp1, &_tmp0), data_length1 = _tmp0),
(data = (g_free (data), NULL)), data = _tmp1, _tmp2)

But as I said.. my current impl is completly broken/ugly/crappy or something.
Comment 5 Vlad Grecescu 2009-01-14 13:17:21 UTC
I recently ran into this too: It seems [1] that on sparc64 the gsize/gint mismatch produces compiling *errors*!

I initially came here to propose gsize by default, but noticing this existing report I realized that some other C APIs may be using a different size type, so..

I just saw 'array_length_type' in current (vala 0.5.5) glib-2.0.vapi. It seems it not used (yet?)

[1] http://code.google.com/p/gtkaml/issues/detail?id=16
Comment 6 Evan Nemerson 2009-08-12 19:50:14 UTC
*** Bug 588104 has been marked as a duplicate of this bug. ***
Comment 7 Mark Lee 2009-08-12 21:18:12 UTC
Created attachment 140589 [details] [review]
Arrays: Add array_length_type annotation to methods

Adds support for the array_length_type CCode annotation, which allows
methods with array length parameters which are not ints to be declared
properly, instead of generating compiler warnings and causing problems
on 64-bit systems (usually signed vs. unsigned).

Testing would be greatly appreciated. It seems to work here on the Vala files where I use KeyFile's get_*_list() methods.
Comment 8 Mark Lee 2009-08-12 21:20:34 UTC
I should probably also mention that this patch adds the Vala.CType class, as juergbi suggested in IRC, to work around the Vala type limitations.
Comment 9 Mark Lee 2009-08-13 20:35:01 UTC
Created attachment 140702 [details] [review]
updated based on juergbi's comments on IRC

Moved valactype.vala from vala/ to codegen/, and updated the commit message.
Comment 10 Jürg Billeter 2009-08-15 12:09:53 UTC
commit 74f2e47906a490f6ae11c058818afc761709a34c
Author: Mark Lee <marklee@src.gnome.org>
Date:   Wed Aug 12 13:53:25 2009 -0700

    Arrays: Add array_length_type annotation to methods
    
    Adds support for the array_length_type CCode annotation, which allows
    methods with non-int array length parameters to be declared properly.
    Fixes possible crashes.
    
    Also adds Vala.CType, to work around the temporary variable type
    limitations.
    
    This fix only works with array return types, not with array parameters.
    
    Partially fixes bug 529866.
Comment 11 Evan Nemerson 2010-02-18 09:42:15 UTC
Created attachment 154111 [details] [review]
Support array_length_type for fields.

I'm not sure what the right way to do this would be for fields. The attached patch simply casts to gint. Even if that's not the right thing to do, this should be a good start since it provides the infrastructure for dealing with array_length_type in Vala.Field.
Comment 12 Jürg Billeter 2010-03-13 17:06:22 UTC
commit 1803fb26dffce892ca6fbf258970e56bed06b0dd
Author: Evan Nemerson <evan@coeus-group.com>
Date:   Sat Mar 13 18:05:14 2010 +0100

    Support array_length_type for fields
    
    Fixes part of bug 529866.
Comment 13 Jürg Billeter 2010-03-26 19:13:51 UTC
*** Bug 601257 has been marked as a duplicate of this bug. ***
Comment 14 Evan Nemerson 2010-04-03 05:46:21 UTC
Created attachment 157786 [details] [review]
support array_length_type for method arguments

Not particularly elegant, but it is functional.
Comment 15 Jürg Billeter 2010-04-27 09:03:25 UTC
commit 7aba22e37c306d3e53a3937827903796e808dd58
Author: Evan Nemerson <evan@coeus-group.com>
Date:   Fri Apr 2 22:33:11 2010 -0700

    Support array_length_type for method parameters
    
    Fixes bug 529866.