From: Rico Tzschichholz Date: Fri, 26 Jun 2020 06:43:12 +0000 (+0200) Subject: vala: Improve parameter check of "get" method meant to be used by foreach X-Git-Tag: 0.46.11~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=908074bf6ea09167da46cecdf41b1147f92a530a;p=thirdparty%2Fvala.git 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 --- 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 { + public K key { get; private set; } + public V value { get; private set; } + + public FooEntry4 (K _key, V _value) { + key = _key; + value = _value; + } +} + +class FooIterator4 { + bool called = false; + + public bool next () { + return !called; + } + + public G @get () { + assert (!called); + called = true; + return new FooEntry4 ("foo", foo_instance); + } +} + +class FooCollection4 { + public int size { get { return 1; } } + + public V @get (K key) { + assert (key == "foo"); + return foo_instance; + } + + public FooIterator4> iterator () { + return new FooIterator4> (); + } +} + Foo foo_instance; void main () { @@ -70,6 +107,14 @@ void main () { assert (foo3 == foo_instance); } + // Uses iterator() and get() + var collection4 = new FooCollection4 (); + 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 (); list.append (foo_instance); diff --git a/vala/valaforeachstatement.vala b/vala/valaforeachstatement.vala index 1ae237cf7..db06160b0 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) { + var 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;