[vala/staging: 2/4] vala: Report dedicated error for static field/property initializer in struct



commit 1a4a14d5550bb23414c0dc66e8951f4b03bd4137
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Nov 29 13:46:21 2018 +0100

    vala: Report dedicated error for static field/property initializer in struct
    
    This fixes criticals and doesn't rely on codegen error reporting.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/446

 tests/Makefile.am                                     |  2 ++
 tests/structs/struct-static-field-initializer.test    |  9 +++++++++
 tests/structs/struct-static-property-initializer.test |  9 +++++++++
 vala/valastruct.vala                                  | 18 ++++++++++++++++++
 4 files changed, 38 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6a85be967..02831dc16 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -189,6 +189,8 @@ TESTS = \
        structs/struct_only.vala \
        structs/struct-empty-still.test \
        structs/struct-no-gtype.vala \
+       structs/struct-static-field-initializer.test \
+       structs/struct-static-property-initializer.test \
        structs/structs.vala \
        structs/gvalue.vala \
        structs/bug530605.vala \
diff --git a/tests/structs/struct-static-field-initializer.test 
b/tests/structs/struct-static-field-initializer.test
new file mode 100644
index 000000000..3e28b0949
--- /dev/null
+++ b/tests/structs/struct-static-field-initializer.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+struct Foo {
+       public int i;
+       public static string bar = "Foobar";
+}
+
+void main () {
+}
diff --git a/tests/structs/struct-static-property-initializer.test 
b/tests/structs/struct-static-property-initializer.test
new file mode 100644
index 000000000..05b7e33b6
--- /dev/null
+++ b/tests/structs/struct-static-property-initializer.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+struct Foo {
+       public int i;
+       public static string bar { get; set; default = "Foobar"; }
+}
+
+void main () {
+}
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index ca187b56c..9b5a02a9a 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -31,6 +31,7 @@ public class Vala.Struct : TypeSymbol {
        private List<Field> fields = new ArrayList<Field> ();
        private List<Method> methods = new ArrayList<Method> ();
        private List<Property> properties = new ArrayList<Property> ();
+       private Set<weak Field> property_fields = new HashSet<weak Field> ();
        private DataType _base_type = null;
 
        private bool? boolean_type;
@@ -282,6 +283,7 @@ public class Vala.Struct : TypeSymbol {
 
                if (prop.field != null) {
                        add_field (prop.field);
+                       property_fields.add (prop.field);
                }
        }
 
@@ -515,6 +517,14 @@ public class Vala.Struct : TypeSymbol {
                                Report.error (f.source_reference, "Instance field initializers not 
supported");
                                return false;
                        }
+
+                       if (f.binding == MemberBinding.STATIC && f.initializer != null) {
+                               // for backing property fields a dedicated error will be reported later
+                               if (!(f in property_fields) && f.variable_type.is_disposable () && 
f.variable_type.value_owned) {
+                                       error = true;
+                                       Report.error (f.initializer.source_reference, "Owned static struct 
fields can only be initialized in a function or method");
+                               }
+                       }
                }
 
                foreach (Constant c in constants) {
@@ -527,6 +537,14 @@ public class Vala.Struct : TypeSymbol {
 
                foreach (Property prop in properties) {
                        prop.check (context);
+
+                       if (prop.binding == MemberBinding.STATIC) {
+                               unowned Field? field = prop.field;
+                               if (field != null && field.initializer != null && 
field.variable_type.is_disposable () && field.variable_type.value_owned) {
+                                       error = true;
+                                       Report.error (field.initializer.source_reference, "Owned static 
struct properties can only be initialized in a function or method");
+                               }
+                       }
                }
 
                if (!external && !external_package) {


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