]> 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>
Mon, 20 Dec 2021 18:24:19 +0000 (19:24 +0100)
tests/Makefile.am
tests/enums/unsafe-assignment.vala [new file with mode: 0644]
vala/valaassignment.vala
vala/valadatatype.vala
vala/valalocalvariable.vala

index 25a7dc886722886e25e6147c7f0dfc3ba5a6d301..6da0d3cdfe7ec33d8c41d61fc643f666bc150a02 100644 (file)
@@ -334,6 +334,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.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..8c0948ef3f61623caad67f8a90c7aea58856b01f 100644 (file)
@@ -320,6 +320,10 @@ 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
+                                   && (!(right is IntegerLiteral) || ((IntegerLiteral) right).value != "0")) {
+                                       //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 8ad4cc0cc3e16683caf3a24047e13b534ba5317a..3b93688e5322b0fa9f0cff2754c400a7d1291f67 100644 (file)
@@ -323,6 +323,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 b4fe16ba4fc17ba5e01bc18e8d1216f5d225027e..7bd665cb394cdd9b953b5d5b990e692b2b78f03d 100644 (file)
@@ -215,6 +215,10 @@ 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
+                           && (!(initializer is IntegerLiteral) || ((IntegerLiteral) initializer).value != "0")) {
+                               //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) {