GNOME Bugzilla – Bug 618857
Invalid pointer asignment in structure initialization
Last modified: 2014-06-27 02:05:54 UTC
Here's a sample code that passes vala compiler, but fails when compiling C: struct Foo { string foo; void *ptr; } void *poing = null; const Foo foo = { "foo", poing }; void main() {} This code doesn't compiles and reports the following error: error: initializer element is not constant (near initialization for â) The fix required to compile in C is: Fixing C code: - const Foo foo = {"foo", poing}; + const Foo foo = {"foo", &poing}; Same for vala: Fixing in Vala code: const Foo foo = { "foo", &poing }; I think vala should allow this syntax and put the '&' in the generated C code.
Maybe this bug is related with https://bugzilla.gnome.org/show_bug.cgi?id=618933
I don't think that valac should implicitly take the address in this case. If you want to store the pointer to the variable instead of the contents, you should really use `&'. The compiler should be fixed to report an error when non-constant expressions are used in constant initializers.
test.vala:3.17-3.17: error: Value must be constant const Foo foo = { "foo", poing }; ^ Compilation failed: 1 error(s), 0 warning(s) It has been doing this for a while, I think since commit 6f12ba028dd6442afa25728e530516af7d8a6f0e Author: Jürg Billeter <j@bitron.ch> Date: Tue Feb 8 19:59:25 2011 +0100 Report error for constants with non-constant expressions