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 621041 - Non-constant Default Arguments Generate Incorrect C
Non-constant Default Arguments Generate Incorrect C
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Methods
0.9.x
Other Linux
: Normal normal
: 1.0
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-06-08 23:46 UTC by Sam
Modified: 2018-05-22 13:36 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Sam 2010-06-08 23:46:38 UTC
When a non-constant argument that references another argument is set in a method definition, any attempt to use that method results in a missing variable.

For example

void test (int[] a, int count = a.length)
{

}

will not compile.

Valac should probably treat this as an error, which it doesn't
Comment 1 Michael 'Mickey' Lauer 2017-11-05 21:44:39 UTC
These days, valac seems to compile your testcase, silently ignoring the non-constant default argument.

I'm leaving this bug open though, since the current behaviour is still not desirable in my opinion.
Comment 2 Al Thomas 2017-11-06 12:23:34 UTC
(In reply to Michael 'Mickey' Lauer from comment #1)
> These days, valac seems to compile your testcase, silently ignoring the
> non-constant default argument.

Default values for function parameters affect Vala code generation on the caller side. It is incorrect to say Vala silently ignores the non-constant default argument. This code:

void main () {
    var test_array = new int[5];
    test (test_array);
}

void test (int[] a, int count = a.length) {
}

Vala master produces this C:

void _vala_main (void) {
        gint* test_array = NULL;
        gint* _tmp0_;
        gint test_array_length1;
        gint _test_array_size_;
        gint* _tmp1_;
        gint _tmp1__length1;
        _tmp0_ = g_new0 (gint, 5);
        test_array = _tmp0_;
        test_array_length1 = 5;
        _test_array_size_ = test_array_length1;
        _tmp1_ = a;
        _tmp1__length1 = a_length1;
        test (test_array, test_array_length1, _tmp1__length1);
        test_array = (g_free (test_array), NULL);
}


void test (gint* a, int a_length1, gint count) {
}

The caller is using the length for the count argument. If that is correct behaviour then this bug is fixed.

> I'm leaving this bug open though, since the current behaviour is still not
> desirable in my opinion.

The more general question is whether a default parameter value should be derived from a required parameter.
Comment 3 Michael 'Mickey' Lauer 2017-11-07 19:36:20 UTC
I'm sorry, you are right. Vala optimized the (unused) function away, when I (successfully) compiled the example from the original post.

The bug is still valid then.

> The more general question is whether a default parameter value should be derived from > a required parameter.

Indeed. To which my personal opinion is NO.

Vala should either support that case or error out with an appropriate message.
Comment 4 GNOME Infrastructure Team 2018-05-22 13:36:57 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/104.