]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Move dynamic property errors to semantic analyzer pass
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 6 Feb 2022 07:57:35 +0000 (08:57 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 9 Feb 2022 21:26:14 +0000 (22:26 +0100)
tests/Makefile.am
tests/objects/property-dynamic-not-supported.test [new file with mode: 0644]
tests/objects/property-dynamic-type-inference.vala [new file with mode: 0644]
vala/valamemberaccess.vala

index 02be6c44fc0f0dcf62ae9ee6595b26bd896caf49..ce61d01157f46b5aa8387bf263fe44d02c3c1b4c 100644 (file)
@@ -512,6 +512,8 @@ TESTS = \
        objects/property-array.vala \
        objects/property-array-length.vala \
        objects/property-base-access.vala \
+       objects/property-dynamic-not-supported.test \
+       objects/property-dynamic-type-inference.vala \
        objects/property-enum.vala \
        objects/property-notify.vala \
        objects/property-notify-owned-getter.vala \
diff --git a/tests/objects/property-dynamic-not-supported.test b/tests/objects/property-dynamic-not-supported.test
new file mode 100644 (file)
index 0000000..15d6590
--- /dev/null
@@ -0,0 +1,10 @@
+Invalid Code
+
+class Foo {
+}
+
+void main () {
+       dynamic Foo foo = new Foo ();
+       foo.bar = "foo";
+       int i = foo.bar;
+}
diff --git a/tests/objects/property-dynamic-type-inference.vala b/tests/objects/property-dynamic-type-inference.vala
new file mode 100644 (file)
index 0000000..3f555da
--- /dev/null
@@ -0,0 +1,25 @@
+class Foo : Object {
+       [CCode (cname = "manam")]
+       public Foo? bar { owned get; set; }
+}
+
+void main () {
+       var foo = new Foo ();
+       assert (foo.ref_count == 1);
+
+       dynamic Foo dfoo = foo;
+       assert (foo.ref_count == 2);
+
+       assert (dfoo.manam == null);
+       assert (foo.ref_count == 2);
+
+       dfoo.manam = foo;
+       assert (foo.ref_count == 3);
+
+       foo = dfoo.manam;
+       assert (foo.ref_count == 3);
+
+       dfoo = null;
+       foo.bar = null;
+       assert (foo.ref_count == 1);
+}
index ab671d026bb9580084edb17e68d7b822464d36a2..d13e322b7926fa4c4fbfa05ad4429aabdc715218 100644 (file)
@@ -478,6 +478,10 @@ public class Vala.MemberAccess : Expression {
                                                prop.owner = inner.value_type.type_symbol.scope;
                                                dynamic_object_type.type_symbol.scope.add (null, prop);
                                                symbol_reference = prop;
+                                               if (!dynamic_object_type.type_symbol.is_subtype_of (context.analyzer.object_type)) {
+                                                       Report.error (source_reference, "dynamic properties are not supported for `%s'", dynamic_object_type.type_symbol.get_full_name ());
+                                                       error = true;
+                                               }
                                        }
                                } else if (parent_node is MemberAccess && inner is MemberAccess && parent_node.parent_node is MethodCall) {
                                        unowned MemberAccess ma = (MemberAccess) parent_node;
@@ -520,6 +524,10 @@ public class Vala.MemberAccess : Expression {
                                        prop.owner = inner.value_type.type_symbol.scope;
                                        dynamic_object_type.type_symbol.scope.add (null, prop);
                                        symbol_reference = prop;
+                                       if (!dynamic_object_type.type_symbol.is_subtype_of (context.analyzer.object_type)) {
+                                               Report.error (source_reference, "dynamic properties are not supported for %s", dynamic_object_type.type_symbol.get_full_name ());
+                                               error = true;
+                                       }
                                }
                                if (symbol_reference != null) {
                                        may_access_instance_members = true;