[vala] codegen: Fix (unowned foo)[] to foo[] array copy



commit a6df1b95af42610a3c580188976a194ac8383d2a
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Aug 12 23:14:25 2014 +0200

    codegen: Fix (unowned foo)[] to foo[] array copy
    
    Fixes bug 731017

 codegen/valaccodebasemodule.vala |   11 ++++++++++-
 tests/Makefile.am                |    1 +
 tests/basic-types/bug731017.vala |   17 +++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 8d6ebdd..e87b0b1 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5743,7 +5743,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        result.lvalue = result.lvalue && result.cvalue == old_cexpr;
                }
 
-               if (!gvalue_boxing && !gvariant_boxing && target_type.value_owned && (!type.value_owned || 
boxing || unboxing) && requires_copy (target_type) && !(type is NullType)) {
+               bool array_needs_copy = false;
+               if (type is ArrayType && target_type is ArrayType) {
+                       var array = (ArrayType) type;
+                       var target_array = (ArrayType) target_type;
+                       if (target_array.element_type.value_owned && !array.element_type.value_owned) {
+                               array_needs_copy = requires_copy (target_array.element_type);
+                       }
+               }
+               
+               if (!gvalue_boxing && !gvariant_boxing && target_type.value_owned && (!type.value_owned || 
boxing || unboxing || array_needs_copy) && requires_copy (target_type) && !(type is NullType)) {
                        // need to copy value
                        var copy = (GLibValue) copy_value (result, node);
                        if (target_type.data_type is Interface && copy == null) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8143bda..3dab72c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -38,6 +38,7 @@ TESTS = \
        basic-types/bug678791.vala \
        basic-types/bug686336.vala \
        basic-types/bug729907.vala \
+       basic-types/bug731017.vala \
        namespaces.vala \
        methods/lambda.vala \
        methods/closures.vala \
diff --git a/tests/basic-types/bug731017.vala b/tests/basic-types/bug731017.vala
new file mode 100644
index 0000000..0b1e0ac
--- /dev/null
+++ b/tests/basic-types/bug731017.vala
@@ -0,0 +1,17 @@
+[CCode (cname = "g_hash_table_get_keys_as_array", array_length_type = "guint", type = "gpointer*")]
+public extern (unowned string)[] g_hash_table_get_keys_as_array (GLib.HashTable<string,string> hash_table);
+
+private static int main (string[] args) {
+  var ht = new GLib.HashTable<string,string> (GLib.str_hash, GLib.str_equal);
+  ht["one"] = "hello";
+  ht["two"] = "world";
+
+  string[] keys = g_hash_table_get_keys_as_array (ht);
+  ht = null;
+  
+  foreach (unowned string k in keys) {
+         assert (k == "one" || k == "two");
+  }
+
+  return 0;
+}
\ No newline at end of file


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