]> 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:52:26 +0000 (19:52 +0100)
tests/Makefile.am
tests/enums/unsafe-assignment.vala [new file with mode: 0644]
vala/valaassignment.vala
vala/valadatatype.vala
vala/valalocalvariable.vala

index 7ce1b3b2e1164c8fbc9d583b7e12ad17799f3751..396565164850d52a4dbdb11e556fda06cc0b0dd9 100644 (file)
@@ -330,6 +330,7 @@ TESTS = \
        enums/in-inference.vala \
        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 241ba956816b76c4a8c244b541df31a7f7d02281..b386f7c4cc18e85a09931ae4de8af46078fe386a 100644 (file)
@@ -314,6 +314,10 @@ public class Vala.Assignment : Expression {
                                        error = true;
                                        Report.error (source_reference, "Assignment: Cannot convert from `%s' to `%s'".printf (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'".printf (right.value_type.to_string (), left.value_type.to_string ()));
                                }
 
                                if (!(ma.symbol_reference is Property)) {
index 209a408210900bce013877cedb25966396d73c5d..b465fa0df1a5b55043649d8d73f2dc797b46078d 100644 (file)
@@ -318,6 +318,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 b08f6f7ee4da0495f1990b7e103e32c522a7a197..1e84abc4c35b03d31a755138631b5747c4e76549 100644 (file)
@@ -215,6 +215,10 @@ public class Vala.LocalVariable : Variable {
                                error = true;
                                Report.error (source_reference, "Assignment: Cannot convert from `%s' to `%s'".printf (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'".printf (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) {