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 662198 - increment operators don't work when accessing objects array-like
increment operators don't work when accessing objects array-like
Status: RESOLVED DUPLICATE of bug 663251
Product: vala
Classification: Core
Component: Parser
0.14.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-10-19 14:19 UTC by Christoph Mende
Modified: 2018-02-21 13:25 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Christoph Mende 2011-10-19 14:19:57 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.
Comment 1 Christoph Mende 2011-10-20 05:51:04 UTC
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.
Comment 2 J.P. Lacerda 2011-11-09 14:59:25 UTC
Seems to be a duplicate of 663251.
Comment 3 Roman Yepishev 2012-04-12 07:43:17 UTC
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.
Comment 4 Michael 'Mickey' Lauer 2018-02-21 13:25:20 UTC

*** This bug has been marked as a duplicate of bug 663251 ***