[vala/wip/issue/442: 2/2] codegen: Transfer memory when casting pointer to non-pointer variable
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/442: 2/2] codegen: Transfer memory when casting pointer to non-pointer variable
- Date: Wed, 1 Dec 2021 20:42:22 +0000 (UTC)
commit f04a01161c7220034b7b4b7390dea9036f9a6f00
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Mar 22 12:29:34 2021 +0100
codegen: Transfer memory when casting pointer to non-pointer variable
codegen/valaccodebasemodule.vala | 5 ++++-
tests/Makefile.am | 2 ++
tests/objects/compact-class-pointer-cast.vala | 20 ++++++++++++++++++++
tests/structs/struct-pointer-cast.vala | 20 ++++++++++++++++++++
4 files changed, 46 insertions(+), 1 deletion(-)
---
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);
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]