[vala/0.48] vala: Correctly replace "in" expression in pre-/postconditions of method



commit a6adb923e570f4a93f0e7ca6cb313454f0f939ce
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Dec 20 20:40:30 2021 +0100

    vala: Correctly replace "in" expression in pre-/postconditions of method
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1269

 tests/Makefile.am                             |  1 +
 tests/methods/prepostconditions-contains.vala | 13 +++++++++++++
 vala/valamethod.vala                          | 17 +++++++++++++++++
 3 files changed, 31 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0087ad5f1..727d3e8c0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -183,6 +183,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
index 000000000..9135da3c7
--- /dev/null
+++ b/tests/methods/prepostconditions-contains.vala
@@ -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 ();
+}
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index d7aeb33bf..a5dd9ad22 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -575,6 +575,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;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]