summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2022-03-29 16:33:55 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2022-03-29 16:44:46 +0200
commit53b003b3a12b1ed5e36b7a4428b19eb578ed36e4 (patch)
tree0dd72d3209c2d88b3f1d25c01dd7bb17769ace39
parent0e822dff98c92a888a10c0d9fe04c724f95a0d79 (diff)
downloadvala-53b003b3a12b1ed5e36b7a4428b19eb578ed36e4.tar.gz
vala: Don't allow nullable enum value as real GObject property
It is basically a boxed integer value. Fixes https://gitlab.gnome.org/GNOME/vala/issues/1074
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/objects/property-enum-nullable.c-expected249
-rw-r--r--tests/objects/property-enum-nullable.vala10
-rw-r--r--vala/valasemanticanalyzer.vala4
4 files changed, 264 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0be8460f2..7666b7c29 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -552,6 +552,7 @@ TESTS = \
objects/property-construct-only-write-foreign.test \
objects/property-delegate.vala \
objects/property-delegate-owned.vala \
+ objects/property-enum-nullable.vala \
objects/property-gboxed-nullable.vala \
objects/property-interface-invalid-name.test \
objects/property-real-struct-assignment.vala \
diff --git a/tests/objects/property-enum-nullable.c-expected b/tests/objects/property-enum-nullable.c-expected
new file mode 100644
index 000000000..38b6bf2d8
--- /dev/null
+++ b/tests/objects/property-enum-nullable.c-expected
@@ -0,0 +1,249 @@
+/* objects_property_enum_nullable.c generated by valac, the Vala compiler
+ * generated from objects_property_enum_nullable.vala, do not modify */
+
+#include <glib-object.h>
+#include <glib.h>
+#include <string.h>
+
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+typedef enum {
+ BAR_Manam
+} Bar;
+
+#define TYPE_BAR (bar_get_type ())
+
+#define TYPE_FOO (foo_get_type ())
+#define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FOO, Foo))
+#define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FOO, FooClass))
+#define IS_FOO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FOO))
+#define IS_FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FOO))
+#define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FOO, FooClass))
+
+typedef struct _Foo Foo;
+typedef struct _FooClass FooClass;
+typedef struct _FooPrivate FooPrivate;
+enum {
+ FOO_0_PROPERTY,
+ FOO_NUM_PROPERTIES
+};
+static GParamSpec* foo_properties[FOO_NUM_PROPERTIES];
+#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
+
+struct _Foo {
+ GObject parent_instance;
+ FooPrivate * priv;
+};
+
+struct _FooClass {
+ GObjectClass parent_class;
+};
+
+struct _FooPrivate {
+ Bar* _bar;
+};
+
+static gint Foo_private_offset;
+static gpointer foo_parent_class = NULL;
+
+VALA_EXTERN GType bar_get_type (void) G_GNUC_CONST ;
+VALA_EXTERN GType foo_get_type (void) G_GNUC_CONST ;
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (Foo, g_object_unref)
+VALA_EXTERN Foo* foo_new (void);
+VALA_EXTERN Foo* foo_construct (GType object_type);
+VALA_EXTERN Bar* foo_get_bar (Foo* self);
+static Bar* _bar_dup (Bar* self);
+VALA_EXTERN void foo_set_bar (Foo* self,
+ Bar* value);
+static void foo_finalize (GObject * obj);
+static GType foo_get_type_once (void);
+static void _vala_foo_get_property (GObject * object,
+ guint property_id,
+ GValue * value,
+ GParamSpec * pspec);
+static void _vala_foo_set_property (GObject * object,
+ guint property_id,
+ const GValue * value,
+ GParamSpec * pspec);
+static void _vala_main (void);
+
+static GType
+bar_get_type_once (void)
+{
+ static const GEnumValue values[] = {{BAR_Manam, "BAR_Manam", "manam"}, {0, NULL, NULL}};
+ GType bar_type_id;
+ bar_type_id = g_enum_register_static ("Bar", values);
+ return bar_type_id;
+}
+
+GType
+bar_get_type (void)
+{
+ static volatile gsize bar_type_id__once = 0;
+ if (g_once_init_enter (&bar_type_id__once)) {
+ GType bar_type_id;
+ bar_type_id = bar_get_type_once ();
+ g_once_init_leave (&bar_type_id__once, bar_type_id);
+ }
+ return bar_type_id__once;
+}
+
+static inline gpointer
+foo_get_instance_private (Foo* self)
+{
+ return G_STRUCT_MEMBER_P (self, Foo_private_offset);
+}
+
+Foo*
+foo_construct (GType object_type)
+{
+ Foo * self = NULL;
+ self = (Foo*) g_object_new (object_type, NULL);
+ return self;
+}
+
+Foo*
+foo_new (void)
+{
+ return foo_construct (TYPE_FOO);
+}
+
+static Bar*
+_bar_dup (Bar* self)
+{
+ Bar* dup;
+ dup = g_new0 (Bar, 1);
+ memcpy (dup, self, sizeof (Bar));
+ return dup;
+}
+
+static gpointer
+__bar_dup0 (gpointer self)
+{
+ return self ? _bar_dup (self) : NULL;
+}
+
+Bar*
+foo_get_bar (Foo* self)
+{
+ Bar* result;
+ Bar* _tmp0_;
+ Bar* _tmp1_;
+ g_return_val_if_fail (IS_FOO (self), NULL);
+ _tmp0_ = self->priv->_bar;
+ _tmp1_ = __bar_dup0 (_tmp0_);
+ result = _tmp1_;
+ return result;
+}
+
+void
+foo_set_bar (Foo* self,
+ Bar* value)
+{
+ Bar* _tmp0_;
+ g_return_if_fail (IS_FOO (self));
+ _tmp0_ = __bar_dup0 (value);
+ _g_free0 (self->priv->_bar);
+ self->priv->_bar = _tmp0_;
+}
+
+static void
+foo_class_init (FooClass * klass,
+ gpointer klass_data)
+{
+ foo_parent_class = g_type_class_peek_parent (klass);
+ g_type_class_adjust_private_offset (klass, &Foo_private_offset);
+ G_OBJECT_CLASS (klass)->get_property = _vala_foo_get_property;
+ G_OBJECT_CLASS (klass)->set_property = _vala_foo_set_property;
+ G_OBJECT_CLASS (klass)->finalize = foo_finalize;
+}
+
+static void
+foo_instance_init (Foo * self,
+ gpointer klass)
+{
+ self->priv = foo_get_instance_private (self);
+}
+
+static void
+foo_finalize (GObject * obj)
+{
+ Foo * self;
+ self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_FOO, Foo);
+ _g_free0 (self->priv->_bar);
+ G_OBJECT_CLASS (foo_parent_class)->finalize (obj);
+}
+
+static GType
+foo_get_type_once (void)
+{
+ static const GTypeInfo g_define_type_info = { sizeof (FooClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) foo_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Foo), 0, (GInstanceInitFunc) foo_instance_init, NULL };
+ GType foo_type_id;
+ foo_type_id = g_type_register_static (G_TYPE_OBJECT, "Foo", &g_define_type_info, 0);
+ Foo_private_offset = g_type_add_instance_private (foo_type_id, sizeof (FooPrivate));
+ return foo_type_id;
+}
+
+GType
+foo_get_type (void)
+{
+ static volatile gsize foo_type_id__once = 0;
+ if (g_once_init_enter (&foo_type_id__once)) {
+ GType foo_type_id;
+ foo_type_id = foo_get_type_once ();
+ g_once_init_leave (&foo_type_id__once, foo_type_id);
+ }
+ return foo_type_id__once;
+}
+
+static void
+_vala_foo_get_property (GObject * object,
+ guint property_id,
+ GValue * value,
+ GParamSpec * pspec)
+{
+ Foo * self;
+ self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_FOO, Foo);
+ switch (property_id) {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+_vala_foo_set_property (GObject * object,
+ guint property_id,
+ const GValue * value,
+ GParamSpec * pspec)
+{
+ Foo * self;
+ self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_FOO, Foo);
+ switch (property_id) {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+_vala_main (void)
+{
+}
+
+int
+main (int argc,
+ char ** argv)
+{
+ _vala_main ();
+ return 0;
+}
+
diff --git a/tests/objects/property-enum-nullable.vala b/tests/objects/property-enum-nullable.vala
new file mode 100644
index 000000000..e7206e060
--- /dev/null
+++ b/tests/objects/property-enum-nullable.vala
@@ -0,0 +1,10 @@
+enum Bar {
+ Manam;
+}
+
+class Foo : Object {
+ public Bar? bar { owned get; set; }
+}
+
+void main () {
+}
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index b1378b557..98f8e1cf5 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -493,6 +493,10 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
}
+ if (property_type is EnumValueType) {
+ return !property_type.nullable;
+ }
+
if (property_type is ArrayType && ((ArrayType) property_type).element_type.type_symbol != string_type.type_symbol) {
return false;
}