]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Use correct target/destroy of delegate field initializer
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 31 Jan 2022 15:37:44 +0000 (16:37 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 9 Feb 2022 21:25:10 +0000 (22:25 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1285

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/delegates/class-field-initializer.vala [new file with mode: 0644]

index 6c1a149100c50633653614d2e64be23b3f1ae323..a368894e2835809b70bf0e35a698f3daddc93712 100644 (file)
@@ -1272,10 +1272,19 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                                var delegate_type = (DelegateType) f.variable_type;
                                                if (delegate_type.delegate_symbol.has_target) {
                                                        var field_value = get_field_cvalue (f, load_this_parameter ((TypeSymbol) f.parent_symbol));
-
-                                                       ccode.add_assignment (get_delegate_target_cvalue (field_value), new CCodeIdentifier ("self"));
+                                                       var target_cvalue = get_delegate_target_cvalue (f.initializer.target_value);
+                                                       if (target_cvalue != null) {
+                                                               ccode.add_assignment (get_delegate_target_cvalue (field_value), target_cvalue);
+                                                       } else {
+                                                               ccode.add_assignment (get_delegate_target_cvalue (field_value), new CCodeIdentifier ("self"));
+                                                       }
                                                        if (delegate_type.is_disposable ()) {
-                                                               ccode.add_assignment (get_delegate_target_destroy_notify_cvalue (field_value), new CCodeConstant ("NULL"));
+                                                               var destroy_cvalue = get_delegate_target_destroy_notify_cvalue (f.initializer.target_value);
+                                                               if (destroy_cvalue != null) {
+                                                                       ccode.add_assignment (get_delegate_target_destroy_notify_cvalue (field_value), destroy_cvalue);
+                                                               } else {
+                                                                       ccode.add_assignment (get_delegate_target_destroy_notify_cvalue (field_value), new CCodeConstant ("NULL"));
+                                                               }
                                                        }
                                                }
                                        }
index 48369672a79a84bf6d6c35c40ae85e66b79c9423..7b625b142317e7de8b461528850dd5a0498f9d5c 100644 (file)
@@ -409,6 +409,7 @@ TESTS = \
        structs/bug777194.vala \
        structs/bug777194-2.vala \
        delegates/casting.vala \
+       delegates/class-field-initializer.vala \
        delegates/compatible.vala \
        delegates/delegate_only.vala \
        delegates/delegate-recusive.vala \
diff --git a/tests/delegates/class-field-initializer.vala b/tests/delegates/class-field-initializer.vala
new file mode 100644 (file)
index 0000000..5a84e4a
--- /dev/null
@@ -0,0 +1,26 @@
+delegate string FooFunc ();
+
+FooFunc get_func () {
+       var s = "foo";
+       return () => { return s; };
+}
+
+class Foo {
+       public FooFunc func = get_func ();
+}
+
+[Compact]
+class Bar {
+       public FooFunc func = get_func ();
+}
+
+void main () {
+       {
+               var foo = new Foo ();
+               assert (foo.func () == "foo");
+       }
+       {
+               var bar = new Bar ();
+               assert (bar.func () == "foo");
+       }
+}