]> 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, 16 Feb 2022 17:47:39 +0000 (18:47 +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 3d4d7cff3f04bf6bc3f3f7ba7d557a85d744de33..48d9a9b01ac07e0300c46deca584d3e81ee84123 100644 (file)
@@ -506,6 +506,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 9282fd28f911ddaf5bbbfde16f64db065548affd..bdb47fdfbad01cd915cc46d486ff544d6490c0ff 100644 (file)
@@ -457,6 +457,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'".printf (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;
@@ -499,6 +503,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".printf (dynamic_object_type.type_symbol.get_full_name ()));
+                                               error = true;
+                                       }
                                }
                                if (symbol_reference != null) {
                                        may_access_instance_members = true;