]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
GVariant: Fix unboxing of Variant to a real non-null struct
authorLuca Bruno <lucabru@src.gnome.org>
Mon, 17 Oct 2011 10:48:55 +0000 (12:48 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Mon, 17 Oct 2011 11:21:15 +0000 (13:21 +0200)
Fixes bug 661945.

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/bug661945.vala [new file with mode: 0644]

index 4df61b8a1e398c593e880204951fd9b55466b351..b4c5efdd4ca48ca47a8b603e1b721d01f9180b40 100644 (file)
@@ -4576,6 +4576,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (to.is_real_non_null_struct_type ()) {
                        // structs are returned via out parameter
                        cfunc.add_parameter (new CCodeParameter ("result", get_ccode_name (to) + "*"));
+                       ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_cvalue_ (result)));
                } else if (to is ArrayType) {
                        // return array length if appropriate
                        // tmp = _variant_get (variant, &tmp_length);
@@ -4587,11 +4588,20 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        }
                }
 
-               ccode.add_assignment (get_cvalue_ (result), ccall);
+               if (!to.is_real_non_null_struct_type ()) {
+                       ccode.add_assignment (get_cvalue_ (result), ccall);
+               } else {
+                       ccode.add_expression (ccall);
+               }
 
                push_function (cfunc);
 
-               ccode.add_return (deserialize_expression (to, new CCodeIdentifier ("value"), new CCodeIdentifier ("*result")));
+               CCodeExpression func_result = deserialize_expression (to, new CCodeIdentifier ("value"), new CCodeIdentifier ("*result"));
+               if (to.is_real_non_null_struct_type ()) {
+                       ccode.add_assignment (new CCodeIdentifier ("*result"), func_result);
+               } else {
+                       ccode.add_return (func_result);
+               }
 
                pop_function ();
 
index 1ce742170299eaf4242d52782b0709ab5111dfcf..2a896bd2e961bde269b226c403a958e196e535dd 100644 (file)
@@ -76,6 +76,7 @@ TESTS = \
        structs/bug656693.vala \
        structs/bug658048.vala \
        structs/bug660426.vala \
+       structs/bug661945.vala \
        delegates/delegates.vala \
        delegates/bug539166.vala \
        delegates/bug595610.vala \
diff --git a/tests/structs/bug661945.vala b/tests/structs/bug661945.vala
new file mode 100644 (file)
index 0000000..98ec4a9
--- /dev/null
@@ -0,0 +1,11 @@
+struct Foo {
+       int a;
+       int b;
+}
+
+void main () {
+       Variant a = Foo() { a=2, b=3 };
+       Foo b = (Foo) a;
+       assert (b.a == 2);
+       assert (b.b == 3);
+}