]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: GLib.Value unboxing returns unowned value
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 3 Feb 2021 12:40:23 +0000 (13:40 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 3 Feb 2021 12:40:23 +0000 (13:40 +0100)
tests/structs/gvalue.vala
vala/valacastexpression.vala

index 5283fc8185ff4f1f35e69548d6ab1d15c06a498c..f1b931784334401553bf56fb2cf47d55cc6e95ba 100644 (file)
@@ -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 () {
index 86b9f9a1289f0ffbaadfb56b0610e7c2483ae947..cc3dbca9ebf03db3189c3e37a63cc0f34549d58a 100644 (file)
@@ -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);