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



commit ff67da58d2ac68145dbc7030f019cb3b05d73b7c
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 8a92b7b0a..8ae9193d1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
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 b3bb3794d..49fa6485e 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -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;


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