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



commit a8a3f28463015e7b4ebfce8ce1a7e838dcd66a53
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                                  |  2 ++
 tests/objects/property-read-only-member-write.test | 15 +++++++++++++++
 tests/objects/property-write-only-member-read.test | 10 ++++++++++
 vala/valaassignment.vala                           |  4 ++--
 4 files changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5ca793613..d9c7a24fc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -469,6 +469,7 @@ 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-write.test \
        objects/property-construct-only-write-foreign.test \
@@ -480,6 +481,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-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..6f4c6d2d0 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 == 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]