]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Correctly replace "in" expression in pre-/postconditions of method
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 20 Dec 2021 19:40:30 +0000 (20:40 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 4 Jan 2022 11:27:42 +0000 (12:27 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1269

tests/Makefile.am
tests/methods/prepostconditions-contains.vala [new file with mode: 0644]
vala/valamethod.vala

index 8a92b7b0aea8e81ed1c16314ea279555ac933753..8ae9193d1c039a089b3b00c451871e12c560d4f8 100644 (file)
@@ -184,6 +184,7 @@ TESTS = \
        methods/preconditions-temp-variables.vala \
        methods/prepostconditions.vala \
        methods/prepostconditions-captured.vala \
+       methods/prepostconditions-contains.vala \
        methods/postconditions.vala \
        methods/postconditions-temp-variables.vala \
        methods/return-unowned-delegate.vala \
diff --git a/tests/methods/prepostconditions-contains.vala b/tests/methods/prepostconditions-contains.vala
new file mode 100644 (file)
index 0000000..9135da3
--- /dev/null
@@ -0,0 +1,13 @@
+const string[] array = { "foo", "bar", "manam" };
+
+void foo (string s) requires (s in array) {
+}
+
+string bar () ensures (result in array) {
+       return "manam";
+}
+
+void main () {
+       foo ("bar");
+       bar ();
+}
index b3bb3794d4f1dfb95d7f6dd1ec58ad4966dbcebd..49fa6485ef88575e051e88cdfe7efd3dc0333485 100644 (file)
@@ -577,6 +577,23 @@ public class Vala.Method : Subroutine, Callable {
                }
        }
 
+       public override void replace_expression (Expression old_node, Expression new_node) {
+               if (preconditions != null) {
+                       var index = preconditions.index_of (old_node);
+                       if (index >= 0) {
+                               preconditions[index] = new_node;
+                               new_node.parent_node = this;
+                       }
+               }
+               if (postconditions != null) {
+                       var index = postconditions.index_of (old_node);
+                       if (index >= 0) {
+                               postconditions[index] = new_node;
+                               new_node.parent_node = this;
+                       }
+               }
+       }
+
        public override void replace_type (DataType old_type, DataType new_type) {
                if (base_interface_type == old_type) {
                        base_interface_type = new_type;