[vala/staging: 1/2] vala: Improve check of context if property is writeable on assignments




commit 8debd42933496c64ed04322eafb4422da4dc406a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Feb 26 13:44:09 2021 +0100

    vala: Improve check of context if property is writeable on assignments

 tests/Makefile.am                                  |  3 +++
 tests/objects/property-construct-only.vala         | 21 +++++++++++++++++++++
 tests/objects/property-read-only-member-write.test | 15 +++++++++++++++
 tests/objects/property-write-only-member-read.test | 10 ++++++++++
 vala/valaassignment.vala                           |  4 ++--
 5 files changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5ca793613..4d7ee3367 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -469,7 +469,9 @@ TESTS = \
        objects/property-notify-owned-getter.vala \
        objects/property-ownership.vala \
        objects/property-read-only-auto.vala \
+       objects/property-read-only-member-write.test \
        objects/property-read-only-write.test \
+       objects/property-construct-only.vala \
        objects/property-construct-only-write.test \
        objects/property-construct-only-write-foreign.test \
        objects/property-delegate.vala \
@@ -480,6 +482,7 @@ TESTS = \
        objects/property-simple-type-struct-nullable.vala \
        objects/property-static.vala \
        objects/property-struct-no-gtype.vala \
+       objects/property-write-only-member-read.test \
        objects/regex.vala \
        objects/sealed-abstract-class.test \
        objects/sealed-class.test \
diff --git a/tests/objects/property-construct-only.vala b/tests/objects/property-construct-only.vala
new file mode 100644
index 000000000..54d040555
--- /dev/null
+++ b/tests/objects/property-construct-only.vala
@@ -0,0 +1,21 @@
+class Foo : GLib.Object {
+       public string manam { get; construct; }
+
+       construct {
+               manam = "foo";
+       }
+}
+
+class Bar : Foo {
+       construct {
+               manam = "bar";
+       }
+}
+
+void main () {
+       var foo = new Foo ();
+       assert (foo.manam == "foo");
+
+       var bar = new Bar ();
+       assert (bar.manam == "bar");
+}
diff --git a/tests/objects/property-read-only-member-write.test 
b/tests/objects/property-read-only-member-write.test
new file mode 100644
index 000000000..6696454e1
--- /dev/null
+++ b/tests/objects/property-read-only-member-write.test
@@ -0,0 +1,15 @@
+Invalid Code
+
+class Foo : GLib.Object {
+       public string manam { get; construct; }
+}
+
+class Bar : GLib.Object {
+       construct {
+               var foo = new Foo ();
+               foo.manam = "manam";
+       }
+}
+
+void main () {
+}
diff --git a/tests/objects/property-write-only-member-read.test 
b/tests/objects/property-write-only-member-read.test
new file mode 100644
index 000000000..213387c35
--- /dev/null
+++ b/tests/objects/property-write-only-member-read.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+class Foo : GLib.Object {
+       public string manam { set; }
+}
+
+void main () {
+       var foo = new Foo ();
+       var s = foo.manam;
+}
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index 4ffc75735..641e18a22 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -277,8 +277,8 @@ public class Vala.Assignment : Expression {
                                        left.value_type = dynamic_prop.property_type.copy ();
                                }
 
-                               if (prop.set_accessor == null
-                                   || (!prop.set_accessor.writable && !(context.analyzer.find_current_method 
() is CreationMethod || context.analyzer.is_in_constructor ()))) {
+                               if (prop.set_accessor == null || (!prop.set_accessor.writable && 
!(context.analyzer.find_current_method () is CreationMethod
+                                   || (context.analyzer.is_in_constructor () && 
context.analyzer.current_type_symbol.is_subtype_of ((TypeSymbol) prop.parent_symbol))))) {
                                        ma.error = true;
                                        Report.error (ma.source_reference, "Property `%s' is read-only", 
prop.get_full_name ());
                                        return false;


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