From: Rico Tzschichholz Date: Wed, 3 Feb 2021 12:40:23 +0000 (+0100) Subject: vala: GLib.Value unboxing returns unowned value X-Git-Tag: 0.51.1~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fbbbb9879880d10b0e2fa064c50fc1dc3d74006;p=thirdparty%2Fvala.git vala: GLib.Value unboxing returns unowned value --- 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);