GNOME Bugzilla – Bug 662198
increment operators don't work when accessing objects array-like
Last modified: 2018-02-21 13:25:20 UTC
Simple example: var table = new HashTable<string,int>(str_hash, str_equal); table["idx"] = 0; table["idx"] += 1; // this works fine table["idx"]++; // this fails with the following error "The expression `GLib.HashTable<string,int>' does not denote an array" I've hit this on Vala 0.14.0.1-e5d2.
Another thing I noted with the first version, this seems to only work on the first call. If I call the function again it still holds a value of 1, only using table["idx"] = table["idx"] + 1; seems to work properly here.
Seems to be a duplicate of 663251.
Same happens with GenericArray and HashTable: using Gee; int main (string[] args) { var arr = new GenericArray<int>(); arr[0] = 1; arr[0] += 1; assert(arr[0] == 2); return 0; } ERROR:/home/rtg/tmp/bugs/array-inc.vala.c:39:_vala_main: assertion failed: (GPOINTER_TO_INT (_tmp1_) == 2) Aborted (core dumped) The resulting C code is wrong: _tmp0_ = g_ptr_array_new_with_free_func (NULL); arr = _tmp0_; // arr[0] = 1 g_ptr_array_set (arr, (guint) 0, GINT_TO_POINTER (1)); // arr[0] += 1 g_ptr_array_set (arr, (guint) 0, GINT_TO_POINTER (1)); _tmp1_ = g_ptr_array_index (arr, (guint) 0); g_assert (GPOINTER_TO_INT (_tmp1_) == 2); With HashMap the code is also wrong: using Gee; int main (string[] args) { var map = new HashMap<string, int?>(); map["test"] = 1; map["test"] += 1; assert(map["test"] == 2); return 0; } Results in: _tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, G_TYPE_INT, (GBoxedCopyFunc) _int_dup, g_free, NULL, NULL, NULL) map = _tmp0_; _tmp1_ = 1; gee_abstract_map_set ((GeeAbstractMap*) map, "test", &_tmp1_); _tmp2_ = 1; gee_abstract_map_set ((GeeAbstractMap*) map, "test", &_tmp2_); _tmp3_ = gee_abstract_map_get ((GeeAbstractMap*) map, "test"); _tmp4_ = (gint*) _tmp3_; _tmp5_ = 2; g_assert (_int_equal (_tmp4_, &_tmp5_) == TRUE); Basically the code for -= and += does not fetch the previous value.
*** This bug has been marked as a duplicate of bug 663251 ***