[vala/0.40] vala: Fix compatible/disposable check between structs and their subtypes



commit e58f888b18833b97a4c4efb32c217808fcaef5a8
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Oct 4 14:12:46 2019 +0200

    vala: Fix compatible/disposable check between structs and their subtypes
    
    This fixes memory leaks due to missing copy/destroy and improperly created
    dup functions and allows direct assignment between structs and their
    subtypes.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/861

 codegen/valaccodestructmodule.vala   |  6 +++++-
 tests/Makefile.am                    |  1 +
 tests/structs/struct-base-types.vala | 31 +++++++++++++++++++++++++++++++
 vala/valadatatype.vala               |  5 +++++
 vala/valastruct.vala                 |  4 ++++
 5 files changed, 46 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala
index 003467bdd..759817e81 100644
--- a/codegen/valaccodestructmodule.vala
+++ b/codegen/valaccodestructmodule.vala
@@ -285,7 +285,11 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                push_function (function);
 
                var dest_struct = new GLibValue (get_data_type_for_symbol (st), new CCodeIdentifier 
("(*dest)"), true);
-               foreach (var f in st.get_fields ()) {
+               unowned Struct sym = st;
+               while (sym.base_struct != null) {
+                       sym = sym.base_struct;
+               }
+               foreach (var f in sym.get_fields ()) {
                        if (f.binding == MemberBinding.INSTANCE) {
                                var value = load_field (f, load_this_parameter ((TypeSymbol) st));
                                if (get_ccode_delegate_target (f) && requires_copy (f.variable_type))  {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 88beb89ec..7f1cc7e97 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -204,6 +204,7 @@ TESTS = \
        enums/bug763831.vala \
        enums/bug780050.vala \
        structs/struct_only.vala \
+       structs/struct-base-types.vala \
        structs/structs.vala \
        structs/gmutex.vala \
        structs/gvalue.vala \
diff --git a/tests/structs/struct-base-types.vala b/tests/structs/struct-base-types.vala
new file mode 100644
index 000000000..928f010bb
--- /dev/null
+++ b/tests/structs/struct-base-types.vala
@@ -0,0 +1,31 @@
+struct Foo {
+       public string s;
+}
+
+struct Bar : Foo {
+}
+
+void main () {
+       {
+               Bar bar = { "bar" };
+               Foo foo = bar;
+
+               assert (bar.s == "bar");
+               assert (bar.s == foo.s);
+
+               void* s1 = foo.s;
+               void* s2 = bar.s;
+               assert (s1 != s2);
+       }
+       {
+               Foo foo = { "foo" };
+               Bar bar = foo;
+
+               assert (foo.s == "foo");
+               assert (foo.s == bar.s);
+
+               void* s1 = foo.s;
+               void* s2 = bar.s;
+               assert (s1 != s2);
+       }
+}
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 67f4185e8..8bdf85bad 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -340,6 +340,11 @@ public abstract class Vala.DataType : CodeNode {
                                        return true;
                                }
                        }
+
+                       // Allow compatiblity of struct subtypes in both ways
+                       if (expect_struct.is_subtype_of (expr_struct)) {
+                               return true;
+                       }
                }
 
                return false;
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index 5e4bcf5b1..bf02c0260 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -449,6 +449,10 @@ public class Vala.Struct : TypeSymbol {
                        return true;
                }
 
+               if (base_struct != null) {
+                       return base_struct.is_disposable ();
+               }
+
                foreach (Field f in fields) {
                        if (f.binding == MemberBinding.INSTANCE
                            && f.get_attribute_bool ("CCode", "delegate_target", true)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]