[vala/staging: 2/4] vala: GLib.Value unboxing returns unowned value




commit 7fbbbb9879880d10b0e2fa064c50fc1dc3d74006
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Feb 3 13:40:23 2021 +0100

    vala: GLib.Value unboxing returns unowned value

 tests/structs/gvalue.vala    | 13 ++++++++++++-
 vala/valacastexpression.vala | 10 ++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/tests/structs/gvalue.vala b/tests/structs/gvalue.vala
index 5283fc818..f1b931784 100644
--- a/tests/structs/gvalue.vala
+++ b/tests/structs/gvalue.vala
@@ -5,6 +5,10 @@ void test_value () {
        string s = "hello";
        Value v2 = s;
        assert (v2.get_string () == s);
+
+       unowned string s2 = "world";
+       Value v3 = s2;
+       assert (v3.get_string () == s2);
 }
 
 void test_value_array () {
@@ -30,6 +34,10 @@ void test_nullable_value () {
        string s = "hello";
        Value? v2 = s;
        assert (v2.get_string () == s);
+
+       unowned string s2 = "world";
+       Value? v3 = s2;
+       assert (v3.get_string () == s2);
 }
 
 void test_nullable_value_array () {
@@ -105,7 +113,10 @@ void test_try_cast_value () {
        Value va = sarray;
 
        string[] sarray2 = (string[]) va;
-       assert (sarray[1] == "vala");
+       assert (sarray2[1] == "vala");
+
+       unowned string[] sarray3 = (string[]) va;
+       assert (sarray3[2] == "world");
 }
 
 void main () {
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index 86b9f9a12..cc3dbca9e 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -211,6 +211,12 @@ public class Vala.CastExpression : Expression {
                        }
                }
 
+               if (context.profile == Profile.GOBJECT
+                   && is_gvalue (context, inner.value_type) && !is_gvalue (context, value_type)) {
+                       // GValue unboxing returns unowned value
+                       value_type.value_owned = false;
+               }
+
                inner.target_type = inner.value_type.copy ();
 
                return !error;
@@ -220,6 +226,10 @@ public class Vala.CastExpression : Expression {
                return type.type_symbol != null && type.type_symbol.is_subtype_of 
(context.analyzer.gvariant_type.type_symbol);
        }
 
+       bool is_gvalue (CodeContext context, DataType type) {
+               return type.type_symbol != null && type.type_symbol.is_subtype_of 
(context.analyzer.gvalue_type.type_symbol);
+       }
+
        public override void emit (CodeGenerator codegen) {
                inner.emit (codegen);
 


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