]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Improve parameter check of "get" method meant to be used by foreach
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 26 Jun 2020 06:43:12 +0000 (08:43 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 26 Jun 2020 06:43:12 +0000 (08:43 +0200)
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
vala/valaforeachstatement.vala

index d2ee2dcc69bf250850e6a73074d07f1e949a4109..009d0221dc13af4b66df3054963a02fe3971aef4 100644 (file)
@@ -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);
index 94fa01578675503d39d461aa0b7394ab2cb8f5c9..c7fb6ee505afd8c47c87b53295ceee3ffe6d40ea 100644 (file)
@@ -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.compatible (context.analyzer.int_type)) {
                        return false;
                }
                var size_property = collection_type.get_member ("size") as Property;