[vala/0.40] vala: Compact classes don't allow private/class fields and to lock fields



commit b4921028ec53af4396fd81217b039272910ec889
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Nov 17 15:02:01 2018 +0100

    vala: Compact classes don't allow private/class fields and to lock fields
    
    This conveniently moves 2 error reports from codegen to semantic check too.

 codegen/valaccodebasemodule.vala                |  6 ------
 codegen/valagtypemodule.vala                    |  4 ----
 tests/Makefile.am                               |  3 +++
 tests/semantic/class-compact-field-class.test   |  9 +++++++++
 tests/semantic/class-compact-field-lock.test    | 13 +++++++++++++
 tests/semantic/class-compact-field-private.test |  9 +++++++++
 vala/valaclass.vala                             | 12 ++++++++++++
 vala/valalockstatement.vala                     |  9 +++++++++
 vala/valaunlockstatement.vala                   |  9 +++++++++
 9 files changed, 64 insertions(+), 10 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 832499587..169b78e23 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1185,12 +1185,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                pop_context ();
                        }
                } else if (f.binding == MemberBinding.CLASS)  {
-                       if (!is_gtypeinstance) {
-                               Report.error (f.source_reference, "class fields are not supported in compact 
classes");
-                               f.error = true;
-                               return;
-                       }
-
                        if (f.access == SymbolAccessibility.PRIVATE) {
                                var ccall = new CCodeFunctionCall (new CCodeIdentifier 
("%s_GET_CLASS_PRIVATE".printf (get_ccode_upper_case_name (cl))));
                                ccall.add_argument (new CCodeIdentifier ("klass"));
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 1484483e4..f9797e18d 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -579,10 +579,6 @@ public class Vala.GTypeModule : GErrorModule {
                                string macro = "(G_TYPE_CLASS_GET_PRIVATE (klass, %s, 
%sClassPrivate))".printf (get_ccode_type_id (cl), get_ccode_name (cl));
                                decl_space.add_type_member_declaration (new CCodeMacroReplacement 
("%s_GET_CLASS_PRIVATE(klass)".printf (get_ccode_upper_case_name (cl, null)), macro));
                        }
-               } else {
-                       if (cl.has_private_fields) {
-                               Report.error (cl.source_reference, "Private fields not supported in compact 
classes");
-                       }
                }
        }
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 89ac09622..9273068a8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -499,6 +499,9 @@ TESTS = \
        semantic/chainup-gobject-unsupported-type-property.test \
        semantic/class-base-type-invalid.test \
        semantic/class-base-type-less-accessible.test \
+       semantic/class-compact-field-class.test \
+       semantic/class-compact-field-lock.test \
+       semantic/class-compact-field-private.test \
        semantic/class-compact-derived-instance-field.test \
        semantic/class-compact-interface.test \
        semantic/class-compact-method-baseaccess.test \
diff --git a/tests/semantic/class-compact-field-class.test b/tests/semantic/class-compact-field-class.test
new file mode 100644
index 000000000..0f00e72d1
--- /dev/null
+++ b/tests/semantic/class-compact-field-class.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+[Compact]
+class Foo {
+       public class int i;
+}
+
+void main () {
+}
diff --git a/tests/semantic/class-compact-field-lock.test b/tests/semantic/class-compact-field-lock.test
new file mode 100644
index 000000000..d4124ee42
--- /dev/null
+++ b/tests/semantic/class-compact-field-lock.test
@@ -0,0 +1,13 @@
+Invalid Code
+
+[Compact]
+class Foo {
+       public int i;
+       public void foo () {
+               lock (i) {
+               }
+       }
+}
+
+void main () {
+}
diff --git a/tests/semantic/class-compact-field-private.test b/tests/semantic/class-compact-field-private.test
new file mode 100644
index 000000000..335638355
--- /dev/null
+++ b/tests/semantic/class-compact-field-private.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+[Compact]
+class Foo {
+       int i;
+}
+
+void main () {
+}
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index 21acf55dc..dafef35ef 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -547,6 +547,18 @@ public class Vala.Class : ObjectTypeSymbol {
                }
 
                foreach (Field f in get_fields ()) {
+                       if (is_compact && f.binding != MemberBinding.STATIC) {
+                               //FIXME Should external bindings follow this too?
+                               if (!external_package && f.access == SymbolAccessibility.PRIVATE) {
+                                       Report.error (source_reference, "private fields are not supported in 
compact classes");
+                                       error = true;
+                               }
+                               if (f.binding == MemberBinding.CLASS) {
+                                       Report.error (f.source_reference, "class fields are not supported in 
compact classes");
+                                       error = true;
+                               }
+                       }
+
                        f.check (context);
                }
                
diff --git a/vala/valalockstatement.vala b/vala/valalockstatement.vala
index e1297ffbc..8a0391e94 100644
--- a/vala/valalockstatement.vala
+++ b/vala/valalockstatement.vala
@@ -118,6 +118,15 @@ public class Vala.LockStatement : CodeNode, Statement {
                        error = true;
                        resource.error = true;
                        Report.error (resource.source_reference, "Only members of the current class are 
lockable");
+                       return false;
+               }
+
+               /* parent class must not be compact */
+               if (context.analyzer.current_class.is_compact) {
+                       error = true;
+                       resource.error = true;
+                       Report.error (resource.source_reference, "Only members of the non-compact classes are 
lockable");
+                       return false;
                }
 
                ((Lockable) resource.symbol_reference).set_lock_used (true);
diff --git a/vala/valaunlockstatement.vala b/vala/valaunlockstatement.vala
index 7cfdf4e18..98a769459 100644
--- a/vala/valaunlockstatement.vala
+++ b/vala/valaunlockstatement.vala
@@ -76,6 +76,15 @@ public class Vala.UnlockStatement : CodeNode, Statement {
                        error = true;
                        resource.error = true;
                        Report.error (resource.source_reference, "Only members of the current class are 
lockable");
+                       return false;
+               }
+
+               /* parent class must not be compact */
+               if (context.analyzer.current_class.is_compact) {
+                       error = true;
+                       resource.error = true;
+                       Report.error (resource.source_reference, "Only members of the non-compact classes are 
lockable");
+                       return false;
                }
 
                ((Lockable) resource.symbol_reference).set_lock_used (true);


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