[vala/0.52] vala: Allow unsafe assignment of integer to enum while reporting a notice



commit 7fab5eaf79fdb794edbc357a21683a0fa2616229
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 notice

 tests/Makefile.am                  |  1 +
 tests/enums/unsafe-assignment.vala | 13 +++++++++++++
 vala/valaassignment.vala           |  4 ++++
 vala/valadatatype.vala             |  3 +++
 vala/valalocalvariable.vala        |  4 ++++
 5 files changed, 25 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 25a7dc886..6da0d3cdf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..b5d3e0551
--- /dev/null
+++ b/tests/enums/unsafe-assignment.vala
@@ -0,0 +1,13 @@
+enum Foo {
+       BAR
+}
+
+void main () {
+       {
+               Foo foo = 23;
+       }
+       {
+               Foo foo = Foo.BAR;
+               foo = 42;
+       }
+}
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index 137361b28..8c0948ef3 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -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)) {
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 8ad4cc0cc..3b93688e5 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -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
diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala
index b4fe16ba4..7bd665cb3 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -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) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]