]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use result value of assignment rather than its computation
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 14 Jan 2020 07:36:26 +0000 (08:36 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 14 Jan 2020 08:03:07 +0000 (09:03 +0100)
An inline assignment of an array-length, like

    int j = --i.length;

resulted in a faulty tranformation

    i_length1 = i_length1 - 1;
    j = i_length1 - 1;

Regression of 80d4bf61e0c3100c839f3fdbcb5218996b6afd5f

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

codegen/valaccodeassignmentmodule.vala
tests/Makefile.am
tests/arrays/length-inline-assignment.vala [new file with mode: 0644]

index 617293b0a67e81382f3293492e38898507e3a6fa..fc7e8d6fc6e201b3d50ac56e91900f657546109e 100644 (file)
@@ -83,7 +83,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
                if (assignment.left.value_type is ArrayType && (((ArrayType) assignment.left.value_type).inline_allocated)) {
                        return load_variable (variable, assignment.left.target_value);
                } else {
-                       return assignment.right.target_value;
+                       return assignment.left.target_value;
                }
        }
 
index 0d4968e75d5e942f26de1daf8d8de9d9536818e1..f7c520de8c2ed7e3575ef25b62a79adfcb553bd2 100644 (file)
@@ -78,6 +78,7 @@ TESTS = \
        arrays/fixed-length-concat-invalid.test \
        arrays/fixed-length-non-const.test \
        arrays/fixed-length-resize-invalid.test \
+       arrays/length-inline-assignment.vala \
        arrays/struct-field-length-cname.vala \
        arrays/incompatible-integer-elements.test \
        arrays/slice-invalid-start.test \
diff --git a/tests/arrays/length-inline-assignment.vala b/tests/arrays/length-inline-assignment.vala
new file mode 100644 (file)
index 0000000..baf3305
--- /dev/null
@@ -0,0 +1,29 @@
+void main () {
+       {
+               int[] i = { 23, 42 };
+               int j = --i.length;
+               assert (i.length == 1);
+               assert (j == 1);
+               j = ++i.length;
+               assert (i.length == 2);
+               assert (j == 2);
+       }
+       {
+               int[] i = { 23, 42 };
+               int j = (i.length = i.length - 1);
+               assert (i.length == 1);
+               assert (j == 1);
+               j = (i.length = i.length + 1);
+               assert (i.length == 2);
+               assert (j == 2);
+       }
+       {
+               int[] i = { 23, 42 };
+               int j = i.length--;
+               assert (i.length == 1);
+               assert (j == 2);
+               j = i.length++;
+               assert (i.length == 2);
+               assert (j == 1);
+       }
+}