[vala/0.48] vala: Improve parameter check of "get" method meant to be used by foreach



commit b042e143c3d45acb008c4b34307f6b164f07a550
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Jun 26 08:43:12 2020 +0200

    vala: Improve parameter check of "get" method meant to be used by foreach
    
    The index-based iteration requires the "get" method to take one integer
    compatible parameter. Otherwise continue checking other options.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1017

 tests/methods/iterator.vala    | 45 ++++++++++++++++++++++++++++++++++++++++++
 vala/valaforeachstatement.vala |  3 ++-
 2 files changed, 47 insertions(+), 1 deletion(-)
---
diff --git a/tests/methods/iterator.vala b/tests/methods/iterator.vala
index d2ee2dcc6..009d0221d 100644
--- a/tests/methods/iterator.vala
+++ b/tests/methods/iterator.vala
@@ -47,6 +47,43 @@ class FooCollection3 {
        }
 }
 
+class FooEntry4<K,V> {
+       public K key { get; private set; }
+       public V value { get; private set; }
+
+       public FooEntry4 (K _key, V _value) {
+               key = _key;
+               value = _value;
+       }
+}
+
+class FooIterator4<G> {
+       bool called = false;
+
+       public bool next () {
+               return !called;
+       }
+
+       public G @get () {
+               assert (!called);
+               called = true;
+               return new FooEntry4<string,Foo> ("foo", foo_instance);
+       }
+}
+
+class FooCollection4<K,V> {
+       public int size { get { return 1; } }
+
+       public V @get (K key) {
+               assert (key == "foo");
+               return foo_instance;
+       }
+
+       public FooIterator4<FooEntry4<K,V>> iterator () {
+               return new FooIterator4<FooEntry4<K,V>> ();
+       }
+}
+
 Foo foo_instance;
 
 void main () {
@@ -70,6 +107,14 @@ void main () {
                assert (foo3 == foo_instance);
        }
 
+       // Uses iterator() and get()
+       var collection4 = new FooCollection4<string,Foo> ();
+       foreach (var fooentry4 in collection4) {
+               assert (fooentry4.key == "foo");
+               assert (fooentry4.value == foo_instance);
+       }
+       assert (collection4["foo"] == foo_instance);
+
        // GLib.List
        var list = new List<Foo> ();
        list.append (foo_instance);
diff --git a/vala/valaforeachstatement.vala b/vala/valaforeachstatement.vala
index 1ae237cf7..357ecabee 100644
--- a/vala/valaforeachstatement.vala
+++ b/vala/valaforeachstatement.vala
@@ -196,7 +196,8 @@ public class Vala.ForeachStatement : Block {
                if (get_method == null) {
                        return false;
                }
-               if (get_method.get_parameters ().size != 1) {
+               unowned List<Parameter> parameters = get_method.get_parameters ();
+               if (parameters.size != 1 || !(parameters[0].variable_type is IntegerType)) {
                        return false;
                }
                var size_property = collection_type.get_member ("size") as Property;


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