]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Transfer memory when casting pointer to non-pointer variable wip/issue/442
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 22 Mar 2021 11:29:34 +0000 (12:29 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 1 Dec 2021 20:22:31 +0000 (21:22 +0100)
codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/objects/compact-class-pointer-cast.vala [new file with mode: 0644]
tests/structs/struct-pointer-cast.vala [new file with mode: 0644]

index 814c14c8ef434219d66b5697124e5fb3c2867aad..207063a82ae5ce190f3f7ce78c558b3bc41e58f7 100644 (file)
@@ -4398,7 +4398,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        }
 
                        // memory management, implicit casts, and boxing/unboxing
-                       if (expr.value_type != null) {
+                       if (expr is CastExpression && expr.value_type != null && !(expr.value_type is PointerType)
+                           && ((CastExpression) expr).inner.value_type is PointerType) {
+                               // allow ownership transfer from pointer to non-pointer
+                       } else if (expr.value_type != null) {
                                // FIXME: temporary workaround until the refactoring is complete, not all target_value have a value_type
                                expr.target_value.value_type = expr.value_type;
                                expr.target_value = transform_value (expr.target_value, expr.target_type, expr);
index e4567227a3a7f413b5c79499883168f467d9716c..5c1c7b7c1197c24e3b0b3e9de9f8eabcc4465d53 100644 (file)
@@ -360,6 +360,7 @@ TESTS = \
        structs/struct-initializer-list-nested.vala \
        structs/struct-no-gtype.vala \
        structs/struct-no-gtype-inherit.vala \
+       structs/struct-pointer-cast.vala \
        structs/struct-static-field-initializer.vala \
        structs/struct-static-field-initializer-2.test \
        structs/struct-static-property-initializer.test \
@@ -471,6 +472,7 @@ TESTS = \
        objects/classes-implicit-implementation.vala \
        objects/compact-class.vala \
        objects/compact-class-destructor.vala \
+       objects/compact-class-pointer-cast.vala \
        objects/compact-class-refcount.vala \
        objects/compact-class-custom-ref.vala \
        objects/constructor-abstract-public.test \
diff --git a/tests/objects/compact-class-pointer-cast.vala b/tests/objects/compact-class-pointer-cast.vala
new file mode 100644 (file)
index 0000000..4511a48
--- /dev/null
@@ -0,0 +1,20 @@
+[Compact]
+class Foo {
+       public int i;
+}
+
+void main () {
+       {
+               Foo foo = (Foo) Slice.alloc (sizeof (int));
+               foo.i = 23;
+               unowned Foo foo_r = foo;
+               assert (foo_r.i == 23);
+       }
+       {
+               Foo* foo_p = Slice.alloc (sizeof (int));
+               foo_p->i = 42;
+               Foo foo = (Foo) foo_p;
+               unowned Foo foo_r = foo;
+               assert (foo_r.i == 42);
+       }
+}
diff --git a/tests/structs/struct-pointer-cast.vala b/tests/structs/struct-pointer-cast.vala
new file mode 100644 (file)
index 0000000..92011b0
--- /dev/null
@@ -0,0 +1,20 @@
+[CCode (has_type_id = false)]
+struct Foo {
+       public int i;
+}
+
+void main () {
+       {
+               Foo? foo = (Foo?) GLib.malloc (sizeof (int));
+               foo.i = 23;
+               unowned Foo? foo_r = foo;
+               assert (foo_r.i == 23);
+       }
+       {
+               Foo* foo_p = GLib.malloc (sizeof (int));
+               foo_p->i = 42;
+               Foo? foo = (Foo?) foo_p;
+               unowned Foo? foo_r = foo;
+               assert (foo_r.i == 42);
+       }
+}