[vala/staging: 1/2] vala: Allow unsafe assignment of integer to enum while reporting a warning
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging: 1/2] vala: Allow unsafe assignment of integer to enum while reporting a warning
- Date: Thu, 16 Dec 2021 14:35:36 +0000 (UTC)
commit 34935a80021d26e9e6b4f7e1e5a2dc6662b67885
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Thu Dec 16 15:31:47 2021 +0100
vala: Allow unsafe assignment of integer to enum while reporting a warning
tests/Makefile.am | 1 +
tests/enums/unsafe-assignment.c-expected | 61 ++++++++++++++++++++++++++++++++
tests/enums/unsafe-assignment.vala | 8 +++++
vala/valaassignment.vala | 3 ++
vala/valadatatype.vala | 3 ++
5 files changed, 76 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5a5b7a346..1895d8078 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -344,6 +344,7 @@ TESTS = \
enums/in-invalid.test \
enums/no_gtype_to_string.vala \
enums/switch.vala \
+ enums/unsafe-assignment.vala \
enums/bug614424.vala \
enums/bug666035.vala \
enums/bug666035-1.test \
diff --git a/tests/enums/unsafe-assignment.c-expected b/tests/enums/unsafe-assignment.c-expected
new file mode 100644
index 000000000..e6a09b9ea
--- /dev/null
+++ b/tests/enums/unsafe-assignment.c-expected
@@ -0,0 +1,61 @@
+/* enums_unsafe_assignment.c generated by valac, the Vala compiler
+ * generated from enums_unsafe_assignment.vala, do not modify */
+
+#include <glib-object.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 {
+ FOO_BAR
+} Foo;
+
+#define TYPE_FOO (foo_get_type ())
+
+VALA_EXTERN GType foo_get_type (void) G_GNUC_CONST ;
+static void _vala_main (void);
+
+static GType
+foo_get_type_once (void)
+{
+ static const GEnumValue values[] = {{FOO_BAR, "FOO_BAR", "bar"}, {0, NULL, NULL}};
+ GType foo_type_id;
+ foo_type_id = g_enum_register_static ("Foo", values);
+ return foo_type_id;
+}
+
+GType
+foo_get_type (void)
+{
+ static volatile gsize foo_type_id__volatile = 0;
+ if (g_once_init_enter (&foo_type_id__volatile)) {
+ GType foo_type_id;
+ foo_type_id = foo_get_type_once ();
+ g_once_init_leave (&foo_type_id__volatile, foo_type_id);
+ }
+ return foo_type_id__volatile;
+}
+
+static void
+_vala_main (void)
+{
+ Foo foo = 0;
+ foo = FOO_BAR;
+ foo += 1;
+}
+
+int
+main (int argc,
+ char ** argv)
+{
+ _vala_main ();
+ return 0;
+}
+
diff --git a/tests/enums/unsafe-assignment.vala b/tests/enums/unsafe-assignment.vala
new file mode 100644
index 000000000..20b3f9d85
--- /dev/null
+++ b/tests/enums/unsafe-assignment.vala
@@ -0,0 +1,8 @@
+enum Foo {
+ BAR
+}
+
+void main () {
+ Foo foo = Foo.BAR;
+ foo += 1;
+}
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index 137361b28..8cd17f277 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -320,6 +320,9 @@ public class Vala.Assignment : Expression {
error = true;
Report.error (source_reference, "Assignment: Cannot convert from `%s'
to `%s'", right.value_type.to_string (), left.value_type.to_string ());
return false;
+ } else if (left.value_type is EnumValueType) {
+ //FIXME This will have to be an error in the future?
+ Report.warning (source_reference, "Assignment: Unsafe conversion from
`%s' to `%s'", right.value_type.to_string (), left.value_type.to_string ());
}
if (!(ma.symbol_reference is Property)) {
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 4a0f94a65..d6049235c 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -324,6 +324,9 @@ public abstract class Vala.DataType : CodeNode {
if (type_symbol is Enum && target_type.type_symbol is Struct && ((Struct)
target_type.type_symbol).is_integer_type ()) {
return true;
+ } else if (target_type.type_symbol is Enum && type_symbol is Struct && ((Struct)
type_symbol).is_integer_type ()) {
+ //FIXME Drop this unsafe direction in the future?
+ return true;
}
// check for matching ownership of type-arguments
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]