[vala/staging: 4/4] vala: Array with fixed length don't require explicit instantiation



commit 0554ed7ad9cefe0f56e53dc00be88488dfa788bf
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Dec 16 20:01:19 2018 +0100

    vala: Array with fixed length don't require explicit instantiation
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/720

 tests/Makefile.am                              |  1 +
 tests/basic-types/arrays-fixed-assignment.vala | 27 ++++++++++++++++++++++++++
 vala/valaassignment.vala                       |  9 +++++++++
 vala/valafield.vala                            |  7 +++++++
 vala/valalocalvariable.vala                    |  9 +++++++--
 5 files changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 380703da2..1a18f2c13 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -25,6 +25,7 @@ TESTS = \
        basic-types/floats.vala \
        basic-types/strings.vala \
        basic-types/arrays.vala \
+       basic-types/arrays-fixed-assignment.vala \
        basic-types/array-uint8-uchar-compat.vala \
        basic-types/pointers.vala \
        basic-types/sizeof.vala \
diff --git a/tests/basic-types/arrays-fixed-assignment.vala b/tests/basic-types/arrays-fixed-assignment.vala
new file mode 100644
index 000000000..0ec0df437
--- /dev/null
+++ b/tests/basic-types/arrays-fixed-assignment.vala
@@ -0,0 +1,27 @@
+string foo[3];
+
+void main () {
+       {
+               foo = new string[3];
+       }
+       {
+               foo = new string[3] { "foo", "bar", "baz" };
+               assert (foo[1] == "bar");
+       }
+       {
+               foo = { "foo", "bar", "baz" };
+               assert (foo[1] == "bar");
+       }
+
+       {
+               string bar[3] = new string[3];
+       }
+       {
+               string bar[3] = new string[3] { "foo", "bar", "baz" };
+               assert (bar[1] == "bar");
+       }
+       {
+               string bar[3] = { "foo", "bar", "baz" };
+               assert (bar[1] == "bar");
+       }
+}
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index f672b8177..ce3549aaf 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -305,6 +305,15 @@ public class Vala.Assignment : Expression {
                                        Report.error (source_reference, "Assignment: Invalid assignment 
attempt");
                                        return false;
                                }
+                       } else if (ma.symbol_reference is Variable) {
+                               unowned Variable variable = (Variable) ma.symbol_reference;
+                               unowned ArrayType? variable_array_type = variable.variable_type as ArrayType;
+                               if (variable_array_type != null && variable_array_type.fixed_length
+                                   && right is ArrayCreationExpression && ((ArrayCreationExpression) 
right).initializer_list == null) {
+                                       Report.warning (source_reference, "Arrays with fixed length don't 
require an explicit instantiation");
+                                       ((Block) parent_node.parent_node).replace_statement ((Statement) 
parent_node, new EmptyStatement (source_reference));
+                                       return true;
+                               }
                        }
 
                        if (left.value_type != null && right.value_type != null) {
diff --git a/vala/valafield.vala b/vala/valafield.vala
index f02b09092..6bd54ff95 100644
--- a/vala/valafield.vala
+++ b/vala/valafield.vala
@@ -107,6 +107,13 @@ public class Vala.Field : Variable, Lockable {
                        return false;
                }
 
+               unowned ArrayType? variable_array_type = variable_type as ArrayType;
+               if (variable_array_type != null && variable_array_type.fixed_length
+                   && initializer is ArrayCreationExpression && ((ArrayCreationExpression) 
initializer).initializer_list == null) {
+                       Report.warning (source_reference, "Arrays with fixed length don't require an explicit 
instantiation");
+                       initializer = null;
+               }
+
                if (initializer != null) {
                        initializer.target_type = variable_type;
 
diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala
index 591edb8fb..331cfcc1f 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -145,6 +145,13 @@ public class Vala.LocalVariable : Variable {
                        initializer.target_type = variable_type;
                }
 
+               unowned ArrayType? variable_array_type = variable_type as ArrayType;
+               if (variable_array_type != null && variable_array_type.fixed_length
+                   && initializer is ArrayCreationExpression && ((ArrayCreationExpression) 
initializer).initializer_list == null) {
+                       Report.warning (source_reference, "Arrays with fixed length don't require an explicit 
instantiation");
+                       initializer = null;
+               }
+
                if (initializer != null && !initializer.error) {
                        if (initializer.value_type == null) {
                                if (!(initializer is MemberAccess) && !(initializer is LambdaExpression)) {
@@ -180,8 +187,6 @@ public class Vala.LocalVariable : Variable {
                                return false;
                        }
 
-
-                       ArrayType variable_array_type = variable_type as ArrayType;
                        if (variable_array_type != null && variable_array_type.inline_allocated && 
!variable_array_type.fixed_length && is_initializer_list) {
                                variable_array_type.length = new IntegerLiteral (initializer_size.to_string 
());
                                variable_array_type.fixed_length = true;


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