]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Allow null-type as intializer for static struct fields
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 30 Nov 2018 17:43:05 +0000 (18:43 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 30 Nov 2018 17:50:31 +0000 (18:50 +0100)
See 1a4a14d5550bb23414c0dc66e8951f4b03bd4137

tests/Makefile.am
tests/structs/struct-static-field-initializer.vala [new file with mode: 0644]
vala/valastruct.vala

index 02831dc16f9fa5630251c98df3e70194362bc892..a785038b4651daaa5fc5bd5e43557d2742344f6e 100644 (file)
@@ -189,6 +189,7 @@ TESTS = \
        structs/struct_only.vala \
        structs/struct-empty-still.test \
        structs/struct-no-gtype.vala \
+       structs/struct-static-field-initializer.vala \
        structs/struct-static-field-initializer.test \
        structs/struct-static-property-initializer.test \
        structs/structs.vala \
diff --git a/tests/structs/struct-static-field-initializer.vala b/tests/structs/struct-static-field-initializer.vala
new file mode 100644 (file)
index 0000000..35a7a13
--- /dev/null
@@ -0,0 +1,8 @@
+struct Foo {
+       public int i;
+       public static int foo = 42;
+       public static string? bar = null;
+}
+
+void main () {
+}
index 9b5a02a9a1a964844adaf447be5cb62af9457f74..c96632805221ba5e52d61ab56d538b28490e6cb7 100644 (file)
@@ -520,7 +520,7 @@ public class Vala.Struct : TypeSymbol {
 
                        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) {
+                               if (!(f in property_fields) && !(f.initializer.value_type is NullType) && 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");
                                }
@@ -540,7 +540,7 @@ public class Vala.Struct : TypeSymbol {
 
                        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) {
+                               if (field != null && field.initializer != null && !(field.initializer.value_type is NullType) && 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");
                                }