]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Correctly handle pre/post-increment expression as index of element access
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 8 Mar 2023 19:16:51 +0000 (20:16 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 8 Mar 2023 19:22:38 +0000 (20:22 +0100)
Regression of cb1828cfc5273aca752de9b39a77e0cd53305e61

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1417

tests/Makefile.am
tests/basic-types/bug596637.c-expected
tests/control-flow/pre-post-increment-array-index.c-expected [new file with mode: 0644]
tests/control-flow/pre-post-increment-array-index.vala [new file with mode: 0644]
vala/valamemberaccess.vala

index 9257e61ae6c661537dddb9324bea5685b58be9de..499d7f0d431c2b4ef25346263360c6be8a70357c 100644 (file)
@@ -311,6 +311,7 @@ TESTS = \
        control-flow/nested-conditional.vala \
        control-flow/null-conditional-bool.vala \
        control-flow/pre-post-increment.vala \
+       control-flow/pre-post-increment-array-index.vala \
        control-flow/pre-post-increment-field.vala \
        control-flow/pre-post-increment-local.vala \
        control-flow/pre-post-increment-parameter.vala \
index 5d29263b77e67f955db2d33bae12b060923003da..2bdb6d02c56d6c886d31f90936a6b8e35a9a6cfe 100644 (file)
@@ -17,20 +17,16 @@ _vala_main (void)
        gint* _tmp0_;
        gint a_length1;
        gint _a_size_;
-       gint* _tmp1_;
-       gint _tmp1__length1;
+       gint _tmp1_;
        gint _tmp2_;
-       gint _tmp3_;
        _tmp0_ = g_new0 (gint, 1);
        a = _tmp0_;
        a_length1 = 1;
        _a_size_ = a_length1;
-       _tmp1_ = a;
-       _tmp1__length1 = a_length1;
-       _tmp2_ = _tmp1_[0];
-       _tmp1_[0] = _tmp2_ + 1;
-       _tmp3_ = a[0];
-       _vala_assert (_tmp3_ == 1, "a[0] == 1");
+       _tmp1_ = a[0];
+       a[0] = _tmp1_ + 1;
+       _tmp2_ = a[0];
+       _vala_assert (_tmp2_ == 1, "a[0] == 1");
        a = (g_free (a), NULL);
 }
 
diff --git a/tests/control-flow/pre-post-increment-array-index.c-expected b/tests/control-flow/pre-post-increment-array-index.c-expected
new file mode 100644 (file)
index 0000000..da5169e
--- /dev/null
@@ -0,0 +1,45 @@
+/* control_flow_pre_post_increment_array_index.c generated by valac, the Vala compiler
+ * generated from control_flow_pre_post_increment_array_index.vala, do not modify */
+
+#include <glib.h>
+
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
+
+static void _vala_main (void);
+
+static void
+_vala_main (void)
+{
+       gchar foo[4] = {0};
+       gint i = 0;
+       gint _tmp0_;
+       gint _tmp1_;
+       gint _tmp2_;
+       foo[4] = '\0';
+       i = 0;
+       _tmp0_ = i;
+       i = _tmp0_ + 1;
+       foo[_tmp0_] = 'b';
+       i = 0;
+       i = i + 1;
+       _tmp1_ = i;
+       foo[_tmp1_] = 'a';
+       i = 2;
+       _tmp2_ = i;
+       i = _tmp2_ + 1;
+       foo[_tmp2_] = 'r';
+       _vala_assert (i == 3, "i == 3");
+       _vala_assert (g_strcmp0 ((const gchar*) foo, "bar") == 0, "(string) foo == \"bar\"");
+}
+
+int
+main (int argc,
+      char ** argv)
+{
+       _vala_main ();
+       return 0;
+}
+
diff --git a/tests/control-flow/pre-post-increment-array-index.vala b/tests/control-flow/pre-post-increment-array-index.vala
new file mode 100644 (file)
index 0000000..c8a08fe
--- /dev/null
@@ -0,0 +1,14 @@
+void main () {
+       char foo[4];
+       foo[4] = '\0';
+
+       var i = 0;
+       foo[i++] = 'b';
+       i = 0;
+       foo[++i] = 'a';
+       i = 2;
+       foo[i++] = 'r';
+
+       assert (i == 3);
+       assert ((string) foo == "bar");
+}
index 3b91ece73a37d1c451edd2f5dddfeb2e301e9eff..2b75adcc5545011eaf6a6ff3c085497658fed91b 100644 (file)
@@ -1200,7 +1200,7 @@ public class Vala.MemberAccess : Expression {
 
        bool is_tainted () {
                unowned CodeNode node = this;
-               if (node.parent_node is MemberAccess) {
+               if (node.parent_node is ElementAccess || node.parent_node is MemberAccess) {
                        return false;
                }