]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Allow unsafe assignment of integer to enum while reporting a notice
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 16 Dec 2021 14:31:47 +0000 (15:31 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 16 Dec 2021 15:15:44 +0000 (16:15 +0100)
tests/Makefile.am
tests/enums/unsafe-assignment.c-expected [new file with mode: 0644]
tests/enums/unsafe-assignment.vala [new file with mode: 0644]
vala/valaassignment.vala
vala/valadatatype.vala
vala/valalocalvariable.vala

index 5a5b7a34611f842bcd65dfe095b4c43928cb8a33..1895d807884b1f50a756276ef32af6c79ade8dd1 100644 (file)
@@ -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 (file)
index 0000000..5da9d05
--- /dev/null
@@ -0,0 +1,67 @@
+/* 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 = 23;
+       }
+       {
+               Foo foo = 0;
+               foo = FOO_BAR;
+               foo = 42;
+       }
+}
+
+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 (file)
index 0000000..b5d3e05
--- /dev/null
@@ -0,0 +1,13 @@
+enum Foo {
+       BAR
+}
+
+void main () {
+       {
+               Foo foo = 23;
+       }
+       {
+               Foo foo = Foo.BAR;
+               foo = 42;
+       }
+}
index 137361b28ab4919b7a304e45b2da2fa93c45f7c2..fdec022b6f17ddf07e1cea7484db6ef032fe8ee7 100644 (file)
@@ -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 && right.value_type is IntegerType) {
+                                       //FIXME This will have to be an error in the future?
+                                       Report.notice (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)) {
index 4a0f94a65d9c6bd746b6fc47dee88dc9ed42e8c8..d6049235c8aaea9fcccc24c509c89ccd6ac4b667 100644 (file)
@@ -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
index 024b046fe351c06d59582484f17ac95fa2f5f20a..5988085c73a78a2f7094ea98a6d66585cfed669d 100644 (file)
@@ -219,6 +219,9 @@ public class Vala.LocalVariable : Variable {
                                error = true;
                                Report.error (source_reference, "Assignment: Cannot convert from `%s' to `%s'", initializer.value_type.to_string (), variable_type.to_string ());
                                return false;
+                       } else if (variable_type is EnumValueType && initializer.value_type is IntegerType) {
+                               //FIXME This will have to be an error in the future?
+                               Report.notice (source_reference, "Assignment: Unsafe conversion from `%s' to `%s'", initializer.value_type.to_string (), variable_type.to_string ());
                        }
 
                        if (variable_array_type != null && variable_array_type.inline_allocated && !variable_array_type.fixed_length && is_initializer_list) {