diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-03-22 12:29:34 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-12-01 21:22:31 +0100 |
commit | f04a01161c7220034b7b4b7390dea9036f9a6f00 (patch) | |
tree | 59383d5f95f4bd9280b5caf8aa126a3a32fc9b28 | |
parent | ce21d0bf8de2f0876c2c8e86ce7d94e69ddda78a (diff) | |
download | vala-wip/issue/442.tar.gz |
codegen: Transfer memory when casting pointer to non-pointer variablewip/issue/442
-rw-r--r-- | codegen/valaccodebasemodule.vala | 5 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/objects/compact-class-pointer-cast.vala | 20 | ||||
-rw-r--r-- | tests/structs/struct-pointer-cast.vala | 20 |
4 files changed, 46 insertions, 1 deletions
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 814c14c8e..207063a82 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -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); diff --git a/tests/Makefile.am b/tests/Makefile.am index e4567227a..5c1c7b7c1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..4511a4884 --- /dev/null +++ b/tests/objects/compact-class-pointer-cast.vala @@ -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 index 000000000..92011b0e3 --- /dev/null +++ b/tests/structs/struct-pointer-cast.vala @@ -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); + } +} |