diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-02-06 08:57:35 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-02-16 18:47:39 +0100 |
commit | 491ea530de8ee33ee97d0b0cc829467cc10828ec (patch) | |
tree | 33aa3595e5d96461ceee8342d78c44be1c854c96 | |
parent | 48ccb34ced23eb690acfabf3d328effb66371325 (diff) | |
download | vala-491ea530de8ee33ee97d0b0cc829467cc10828ec.tar.gz |
vala: Move dynamic property errors to semantic analyzer pass
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/objects/property-dynamic-not-supported.test | 10 | ||||
-rw-r--r-- | tests/objects/property-dynamic-type-inference.vala | 25 | ||||
-rw-r--r-- | vala/valamemberaccess.vala | 8 |
4 files changed, 45 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 3d4d7cff3..48d9a9b01 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..15d659099 --- /dev/null +++ b/tests/objects/property-dynamic-not-supported.test @@ -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 index 000000000..3f555da3c --- /dev/null +++ b/tests/objects/property-dynamic-type-inference.vala @@ -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); +} diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 9282fd28f..bdb47fdfb 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -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; |