From: Rico Tzschichholz Date: Thu, 29 Nov 2018 12:46:21 +0000 (+0100) Subject: vala: Report dedicated error for static field/property initializer in struct X-Git-Tag: 0.43.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a4a14d5550bb23414c0dc66e8951f4b03bd4137;p=thirdparty%2Fvala.git 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 --- 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 fields = new ArrayList (); private List methods = new ArrayList (); private List properties = new ArrayList (); + private Set property_fields = new HashSet (); 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) {