]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Don't restrict element type of GLib.Array
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 15 Sep 2021 12:38:19 +0000 (14:38 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 15 Sep 2021 12:38:19 +0000 (14:38 +0200)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1227

tests/basic-types/garray.c-expected
tests/basic-types/garray.vala
vala/valasemanticanalyzer.vala

index b98ad90210bba5f45c2761fd3e79f75074ec0369..7a7b656930f42eeb0061a3a8b475b55dc5e4ec2d 100644 (file)
@@ -5,6 +5,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
+#include <float.h>
+#include <math.h>
 
 #if !defined(VALA_EXTERN)
 #if defined(_MSC_VER)
@@ -78,6 +80,8 @@ static void _foo_struct_free0_ (gpointer var);
 static void _vala_FooStruct_free_function_content_of (gpointer data);
 VALA_EXTERN void test_struct_garray (void);
 VALA_EXTERN void test_object_garray (void);
+VALA_EXTERN GArray* check_gvalue_garray (GArray* vals);
+VALA_EXTERN void test_gvalue_garray (void);
 static void _vala_main (void);
 static void _vala_array_destroy (gpointer array,
                           gssize array_length,
@@ -565,6 +569,65 @@ test_object_garray (void)
        _g_object_unref0 (foo);
 }
 
+GArray*
+check_gvalue_garray (GArray* vals)
+{
+       GArray* result = NULL;
+       g_return_val_if_fail (vals != NULL, NULL);
+       _vala_assert (g_strcmp0 (g_value_get_string (&g_array_index (vals, GValue, (guint) 0)), "foo") == 0, "vals.index (0) == \"foo\"");
+       _vala_assert (g_value_get_int (&g_array_index (vals, GValue, (guint) 1)) == 42, "vals.index (1) == 42");
+       _vala_assert (g_value_get_double (&g_array_index (vals, GValue, (guint) 2)) == 3.1415, "vals.index (2) == 3.1415");
+       result = vals;
+       return result;
+}
+
+void
+test_gvalue_garray (void)
+{
+       {
+               GArray* foo = NULL;
+               GArray* _tmp0_;
+               GValue _tmp1_ = {0};
+               GValue _tmp2_ = {0};
+               GValue _tmp3_ = {0};
+               _tmp0_ = g_array_new (TRUE, TRUE, sizeof (GValue));
+               g_array_set_clear_func (_tmp0_, (GDestroyNotify) g_value_unset);
+               foo = _tmp0_;
+               g_value_init (&_tmp1_, G_TYPE_STRING);
+               g_value_set_string (&_tmp1_, "foo");
+               g_array_append_val (foo, _tmp1_);
+               g_value_init (&_tmp2_, G_TYPE_INT);
+               g_value_set_int (&_tmp2_, 42);
+               g_array_append_val (foo, _tmp2_);
+               g_value_init (&_tmp3_, G_TYPE_DOUBLE);
+               g_value_set_double (&_tmp3_, 3.1415);
+               g_array_append_val (foo, _tmp3_);
+               check_gvalue_garray (foo);
+               _g_array_unref0 (foo);
+       }
+       {
+               GArray* foo = NULL;
+               GArray* _tmp4_;
+               GValue _tmp5_ = {0};
+               GValue _tmp6_ = {0};
+               GValue _tmp7_ = {0};
+               _tmp4_ = g_array_new (TRUE, TRUE, sizeof (GValue));
+               g_array_set_clear_func (_tmp4_, (GDestroyNotify) g_value_unset);
+               foo = _tmp4_;
+               g_value_init (&_tmp5_, G_TYPE_STRING);
+               g_value_set_string (&_tmp5_, "foo");
+               g_array_append_val (foo, _tmp5_);
+               g_value_init (&_tmp6_, G_TYPE_INT);
+               g_value_set_int (&_tmp6_, 42);
+               g_array_append_val (foo, _tmp6_);
+               g_value_init (&_tmp7_, G_TYPE_DOUBLE);
+               g_value_set_double (&_tmp7_, 3.1415);
+               g_array_append_val (foo, _tmp7_);
+               check_gvalue_garray (foo);
+               _g_array_unref0 (foo);
+       }
+}
+
 static void
 _vala_main (void)
 {
@@ -572,6 +635,7 @@ _vala_main (void)
        test_int_garray ();
        test_struct_garray ();
        test_object_garray ();
+       test_gvalue_garray ();
 }
 
 int
index 9f09560a7c8b021ee8aedfe9ca83f1eb5e999197..8732fa42fc73ce49075be69105ad6fa2ad4ca130 100644 (file)
@@ -89,9 +89,34 @@ void test_object_garray () {
        assert (foo.ref_count == 1);
 }
 
+unowned Array<Value> check_gvalue_garray (Array<Value> vals) {
+       assert (vals.index (0) == "foo");
+       assert (vals.index (1) == 42);
+       assert (vals.index (2) == 3.1415);
+       return vals;
+}
+
+void test_gvalue_garray () {
+       {
+               var foo = new Array<Value> ();
+               foo.append_val ("foo");
+               foo.append_val (42);
+               foo.append_val (3.1415);
+               check_gvalue_garray (foo);
+       }
+       {
+               Array<Value> foo = new Array<Value> ();
+               foo.append_val ("foo");
+               foo.append_val (42);
+               foo.append_val (3.1415);
+               check_gvalue_garray (foo);
+       }
+}
+
 void main () {
        test_garray ();
        test_int_garray ();
        test_struct_garray ();
        test_object_garray ();
+       test_gvalue_garray ();
 }
index 5536955680b220ffbcfd0dd6a332b1fae0c8e728..016f2ef0264dcdd028819eb8e97936ac01526948 100644 (file)
@@ -1290,6 +1290,12 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        }
 
        public void check_type (DataType type) {
+               // Allow any type-argument for GLib.Array
+               if (context != null && context.profile == Profile.GOBJECT
+                   && type.type_symbol == garray_type.type_symbol) {
+                       return;
+               }
+
                foreach (var type_arg in type.get_type_arguments ()) {
                        check_type (type_arg);
                        check_type_argument (type_arg);