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 643010 - Compilation error when passing a N_ wrapped string to a const array
Compilation error when passing a N_ wrapped string to a const array
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: general
0.12.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-02-22 22:32 UTC by spinatelli.sergio
Modified: 2011-04-05 17:32 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description spinatelli.sergio 2011-02-22 22:32:07 UTC
When compiling (using Vala 0.11.6.18-26eed),some code like this:

const string[] sent_folders = {
        ..., "Gesendet"
    };

const MailFolder[] localized_folders = {
        { "INBOX", STOCK_INBOX, N_("Inbox"), null, 0, FolderType.INBOX, 0 },
        { "Sent", STOCK_SENT_MAIL, N_("Sent"), sent_folders, sent_folders.length, FolderType.SENT, 1 },
        ...
};

it outputs this error:

../postler/postler-accounts.vala:69.44-69.44: error: Value must be constant
Comment 1 spinatelli.sergio 2011-02-22 22:37:28 UTC
Also, removing N_() doesn't fix this problem.
Comment 2 Jürg Billeter 2011-02-23 06:38:14 UTC
As this is not about N_(), I need to know more information how to reproduce the issue. Do you have a test case?
Comment 3 spinatelli.sergio 2011-02-23 11:07:14 UTC
I tried a couple things,reduced the code and got that this small code snippet: http://pastebin.com/XJbRTFVe
doesn't work with 0.11.6,so the problem seems to be passing an array declared as const.
This is the error i get (same as above):

test.vala:23.44-23.44: error: Value must be constant
    const MailFolder[] localized_folders = {
                                           ^
Comment 4 Christian Dywan 2011-03-10 22:10:05 UTC
So I know what detail the problem is: the length values, which, as the comment says, work around an optimisation that breaks later use of the array.

Removing sent_folders.length allows compilation to proceed. But it then triggers the bug where the MailFolder[] array has a length of 0.

public struct MailFolder {
        string[]? localized;
        int n_localized;
    }

    const string[] sent_folders = {
        "[Gmail]~-Sent Mail", "[Gmail]~-Gesendet", "Gesendet"
    };

    /* The length values must be in the array to work around Vala mistakenly
       optimizing the value away when using arrays indirectly with MailFolder */
    const MailFolder[] localized_folders = {
        { null, 0 },
        { sent_folders, sent_folders.length }
    };
}
Comment 5 Michel Alexandre Salim 2011-03-15 18:34:54 UTC
vala 0.11.6 is already shipping in Fedora's Rawhide; Cc-ing myself so we can issue an update when this is fixed.
Comment 6 David Nielsen 2011-03-21 13:34:46 UTC
On a slight tangent, while legal code and thus a proper bug, this is happening because of a workaround. So I looked at the comment and there is no FIXME: with a bug reference so that you will easily know when you can remove the workaround. This has helped us greatly in Banshee with regards to cleaning up our code over time. 

But this that problem even filed?
Comment 7 Michel Alexandre Salim 2011-04-05 09:40:36 UTC
Could the reporter or a developer update the version affected to 0.12.x? It's still a problem with the just-released 0.12.0.

Thanks!
Comment 8 Jürg Billeter 2011-04-05 17:32:38 UTC
This fixes the array length 0 issue. I'm not aware of a separate bug report for it.

commit 5d29984bff7d5c2ac32372470b8de439a477ecd4
Author: Jürg Billeter <j@bitron.ch>
Date:   Tue Apr 5 19:23:00 2011 +0200

    codegen: Fix initializer lists for structs with array fields
Comment 9 Jürg Billeter 2011-04-05 17:32:55 UTC
commit 5d74330d6c660ec7bc6caa6b3c23921b93358204
Author: Jürg Billeter <j@bitron.ch>
Date:   Tue Apr 5 19:28:31 2011 +0200

    Allow access to length of constant array in constant initializer lists
    
    Fixes bug 643010.