GNOME Bugzilla – Bug 684262
wrong code generated for inline array of struct with generic type argument
Last modified: 2018-05-22 14:29:41 UTC
Given the following code: struct Entry<K,V> { K key; V value; } int main (string[] args) { const Entry<int,string>[] map = { { 0, "foo" }, { 1, "bar" } }; stdout.printf ("%d %s\n", map[0].key, map[1].value); return 0; } valac -C generates: typedef struct _Entry Entry; struct _Entry { gpointer key; gpointer value; }; static const Entry map[] = {{0, "foo"}, {1, "bar"}}; // (*) I heard that some compiler does not accept (*)[1]. It seems that a proper cast from integer to pointer (such as "(gintptr)" or "GINT_TO_POINTER") is required. 1. https://github.com/ueno/libskk/issues/20
Thanks for your report. I agree that this might not be safe on all architectures and should be fixed. Apple clang 9.0 reports: % valac bar.vala /tmp/bar.c:40:14: warning: assigning to 'gpointer' (aka 'void *') from 'gconstpointer' (aka 'const void *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers] (*dest).key = _tmp0_; ^ ~~~~~~ /tmp/bar.c:42:16: warning: assigning to 'gpointer' (aka 'void *') from 'gconstpointer' (aka 'const void *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers] (*dest).value = _tmp1_; ^ ~~~~~~ /tmp/bar.c:88:44: warning: incompatible integer to pointer conversion initializing 'gpointer' (aka 'void *') with an expression of type 'int' [-Wint-conversion] static const Entry map[2] = {{0, "foo"}, {1, "bar"}}; ^ 3 warnings generated.
-- 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/320.