[vala/wip/issue/1121: 1/2] vala: Don't allow assigning GtkChild fields/properties




commit 6e7be51cb5455e9c5c721d4ba5121a6ab04673dd
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Jan 16 17:11:14 2021 +0100

    vala: Don't allow assigning GtkChild fields/properties
    
    These are handled exclusively by GtkBuilder
    
    See https://gitlab.gnome.org/GNOME/vala/issues/1121

 tests/Makefile.am                                    |  5 +++++
 tests/gtktemplate/gtkchild-field-assignment.test     | 14 ++++++++++++++
 tests/gtktemplate/gtkchild-field-out-assignment.test | 17 +++++++++++++++++
 tests/gtktemplate/gtkchild-field-ref-assignment.test | 17 +++++++++++++++++
 tests/gtktemplate/gtkchild-property-assignment.test  | 14 ++++++++++++++
 tests/gtktemplate/gtktemplate.vala                   |  2 +-
 tests/gtktemplate/tests-extra-environment.sh         |  2 ++
 vala/valaassignment.vala                             |  6 ++++++
 vala/valaproperty.vala                               |  3 +++
 vala/valaunaryexpression.vala                        |  5 +++++
 10 files changed, 84 insertions(+), 1 deletion(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bca714521..8c001a099 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -709,6 +709,10 @@ TESTS = \
        gir/property-non-readable.test \
        gir/symbol-type-csuffix.test \
        gir/union.test \
+       gtktemplate/gtkchild-field-assignment.test \
+       gtktemplate/gtkchild-field-out-assignment.test \
+       gtktemplate/gtkchild-field-ref-assignment.test \
+       gtktemplate/gtkchild-property-assignment.test \
        annotations/deprecated.vala \
        annotations/description.vala \
        annotations/noaccessormethod.test \
@@ -1152,6 +1156,7 @@ LINUX_TESTS += \
 endif
 
 EXTRA_DIST = \
+       gtktemplate/tests-extra-environment.sh \
        linux/tests-extra-environment.sh \
        nullability/tests-extra-environment.sh \
        posix/tests-extra-environment.sh \
diff --git a/tests/gtktemplate/gtkchild-field-assignment.test 
b/tests/gtktemplate/gtkchild-field-assignment.test
new file mode 100644
index 000000000..4fc71f1d9
--- /dev/null
+++ b/tests/gtktemplate/gtkchild-field-assignment.test
@@ -0,0 +1,14 @@
+Invalid Code
+
+[GtkTemplate (ui = "/org/example/gtktemplate.ui")]
+public class GtkTemplate : Gtk.ApplicationWindow {
+       [GtkChild]
+       public unowned Gtk.Button button0;
+
+       void foo () {
+               button0 = new Gtk.Button ();
+       }
+}
+
+void main () {
+}
diff --git a/tests/gtktemplate/gtkchild-field-out-assignment.test 
b/tests/gtktemplate/gtkchild-field-out-assignment.test
new file mode 100644
index 000000000..3f82f4251
--- /dev/null
+++ b/tests/gtktemplate/gtkchild-field-out-assignment.test
@@ -0,0 +1,17 @@
+Invalid Code
+
+[GtkTemplate (ui = "/org/example/gtktemplate.ui")]
+public class GtkTemplate : Gtk.ApplicationWindow {
+       [GtkChild]
+       public unowned Gtk.Button button0;
+
+       void foo () {
+               bar (out button0);
+       }
+
+       void bar (out unowned Gtk.Button b) {
+       }
+}
+
+void main () {
+}
diff --git a/tests/gtktemplate/gtkchild-field-ref-assignment.test 
b/tests/gtktemplate/gtkchild-field-ref-assignment.test
new file mode 100644
index 000000000..e8dab2e1c
--- /dev/null
+++ b/tests/gtktemplate/gtkchild-field-ref-assignment.test
@@ -0,0 +1,17 @@
+Invalid Code
+
+[GtkTemplate (ui = "/org/example/gtktemplate.ui")]
+public class GtkTemplate : Gtk.ApplicationWindow {
+       [GtkChild]
+       public unowned Gtk.Button button0;
+
+       void foo () {
+               bar (ref button0);
+       }
+
+       void bar (ref unowned Gtk.Button b) {
+       }
+}
+
+void main () {
+}
diff --git a/tests/gtktemplate/gtkchild-property-assignment.test 
b/tests/gtktemplate/gtkchild-property-assignment.test
new file mode 100644
index 000000000..4905ea87c
--- /dev/null
+++ b/tests/gtktemplate/gtkchild-property-assignment.test
@@ -0,0 +1,14 @@
+Invalid Code
+
+[GtkTemplate (ui = "/org/example/gtktemplate.ui")]
+public class GtkTemplate : Gtk.ApplicationWindow {
+       [GtkChild]
+       public unowned Gtk.Button button0 { get; set; }
+
+       void foo () {
+               button0 = new Gtk.Button ();
+       }
+}
+
+void main () {
+}
diff --git a/tests/gtktemplate/gtktemplate.vala b/tests/gtktemplate/gtktemplate.vala
index 5f66ccc21..3919680e2 100644
--- a/tests/gtktemplate/gtktemplate.vala
+++ b/tests/gtktemplate/gtktemplate.vala
@@ -1,7 +1,7 @@
 [GtkTemplate (ui = "/org/example/gtktemplate.ui")]
 public class GtkTemplate : Gtk.ApplicationWindow {
        [GtkChild]
-       public Gtk.Button button0 { get; set; }
+       public Gtk.Button button0 { get; }
 
        [GtkChild (internal = true)]
        public Gtk.Button button1;
diff --git a/tests/gtktemplate/tests-extra-environment.sh b/tests/gtktemplate/tests-extra-environment.sh
new file mode 100644
index 000000000..6c805e648
--- /dev/null
+++ b/tests/gtktemplate/tests-extra-environment.sh
@@ -0,0 +1,2 @@
+PACKAGES="gtk+-3.0"
+VALAFLAGS="--gresources ${abs_srcdir}/gtktemplate/gtktemplate.gresource.xml"
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index 79090ad9b..4ffc75735 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -178,6 +178,12 @@ public class Vala.Assignment : Expression {
                                return false;
                        }
 
+                       if (ma.symbol_reference.get_attribute ("GtkChild") != null) {
+                               error = true;
+                               Report.error (source_reference, "Assignment of [GtkChild] `%s' is not 
allowed", ma.symbol_reference.get_full_name ());
+                               return false;
+                       }
+
                        if (ma.symbol_reference is DynamicProperty) {
                                // target_type not available for dynamic properties
                        } else {
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index 2fc1b4d60..0a2e437d5 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -492,6 +492,9 @@ public class Vala.Property : Symbol, Lockable {
                        get_accessor.check (context);
                }
                if (set_accessor != null) {
+                       if (get_attribute ("GtkChild") != null) {
+                               Report.warning (set_accessor.source_reference, "[GtkChild] property `%s' is 
not allowed to have `set' accessor", get_full_name ());
+                       }
                        set_accessor.check (context);
                }
 
diff --git a/vala/valaunaryexpression.vala b/vala/valaunaryexpression.vala
index 76c31cd7a..4bf6d4bc4 100644
--- a/vala/valaunaryexpression.vala
+++ b/vala/valaunaryexpression.vala
@@ -229,6 +229,11 @@ public class Vala.UnaryExpression : Expression {
                                Report.error (source_reference, "ref and out method arguments can only be 
used with fields, parameters, local variables, and array element access");
                                return false;
                        }
+                       if (inner.symbol_reference.get_attribute ("GtkChild") != null) {
+                               error = true;
+                               Report.error (source_reference, "Assignment of [GtkChild] `%s' is not 
allowed", inner.symbol_reference.get_full_name ());
+                               return false;
+                       }
                        break;
                default:
                        error = true;


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