summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2020-06-26 08:43:12 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2020-06-26 08:43:12 +0200
commitb4b64c9b6e0fef79371a1c446a1777feda8779f1 (patch)
treeac3d4e642c89c6b9245fe1004e49a23aa00fe6ec
parentc6a4b6e95e64a6ebd2bf88d4f3c3a55b2d3f31f4 (diff)
downloadvala-b4b64c9b6e0fef79371a1c446a1777feda8779f1.tar.gz
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
-rw-r--r--tests/methods/iterator.vala45
-rw-r--r--vala/valaforeachstatement.vala3
2 files changed, 47 insertions, 1 deletions
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 94fa01578..c7fb6ee50 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.compatible (context.analyzer.int_type)) {
return false;
}
var size_property = collection_type.get_member ("size") as Property;