GNOME Bugzilla – Bug 643010
Compilation error when passing a N_ wrapped string to a const array
Last modified: 2011-04-05 17:32:55 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
Also, removing N_() doesn't fix this problem.
As this is not about N_(), I need to know more information how to reproduce the issue. Do you have a test case?
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 = { ^
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 } }; }
vala 0.11.6 is already shipping in Fedora's Rawhide; Cc-ing myself so we can issue an update when this is fixed.
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?
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!
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
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.