From: Rico Tzschichholz Date: Mon, 8 Apr 2019 08:00:19 +0000 (+0200) Subject: codegen: Always assign original variable when consuming instance to destroy X-Git-Tag: 0.36.19~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e82e0191e4f5727fac883cbe7021dba1661e21cc;p=thirdparty%2Fvala.git codegen: Always assign original variable when consuming instance to destroy Fixes https://gitlab.gnome.org/GNOME/vala/issues/781 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 2474dc9db..2184cc083 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -3966,15 +3966,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { 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; - - if (is_compact_class_destructor_call (expr)) { - // transfer ownership here and consume given instance - var temp_value = store_temp_value (expr.target_value, expr); - ccode.add_assignment (get_cvalue (expr), new CCodeConstant ("NULL")); - expr.target_value = temp_value; - } else { - expr.target_value = transform_value (expr.target_value, expr.target_type, expr); - } + expr.target_value = transform_value (expr.target_value, expr.target_type, expr); } if (expr.target_value == null) { @@ -3992,6 +3984,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { if (!(expr.value_type is ValueType && !expr.value_type.nullable)) { ((GLibValue) expr.target_value).non_null = expr.is_non_null (); } + } else if (expr.value_type != null && is_compact_class_destructor_call (expr)) { + // transfer ownership here and consume given instance + var temp_value = store_temp_value (expr.target_value, expr); + ccode.add_assignment (get_cvalue (expr), new CCodeConstant ("NULL")); + expr.target_value = temp_value; } } diff --git a/tests/objects/compact-class-destructor.vala b/tests/objects/compact-class-destructor.vala index 2d6d8a442..7aa4604b0 100644 --- a/tests/objects/compact-class-destructor.vala +++ b/tests/objects/compact-class-destructor.vala @@ -9,9 +9,90 @@ class Foo { } } -void main () { +void bar () throws Error { +} + +Foo get_foo () { + return new Foo (); +} + +class Bar { + Foo faz; + + public void instance_simple () { + var foo = new Foo (); + var res = foo.destroy (); + + assert (foo == null); + assert (res == 42); + } + + public void instance_field () { + bar (); + + faz = new Foo (); + var res = faz.destroy (); + + assert (faz == null); + assert (res == 42); + } +} + +Foo faz; + +void field () { + bar (); + + faz = new Foo (); + var res = faz.destroy (); + + assert (faz == null); + assert (res == 42); +} + +void local () { + bar (); + var foo = new Foo (); var res = foo.destroy (); + assert (foo == null); assert (res == 42); } + +void parameter (owned Foo foo) { + bar (); + + var res = foo.destroy (); + + assert (foo == null); + assert (res == 42); +} + +void simple () { + var foo = new Foo (); + var res = foo.destroy (); + + assert (foo == null); + assert (res == 42); +} + +void returned () { + bar (); + + var res = get_foo ().destroy (); + + assert (res == 42); +} + +void main () { + simple (); + field (); + local (); + parameter (new Foo ()); + returned (); + + var bar = new Bar (); + bar.instance_simple (); + bar.instance_field (); +} diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index a57e68cad..55fdaad1c 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -891,6 +891,13 @@ public class Vala.MemberAccess : Expression { ma.check_lvalue_access (); } } + + if (symbol_reference is Method && ((Method) symbol_reference).get_attribute ("DestroysInstance") != null) { + if (ma != null) { + ma.lvalue = true; + ma.check_lvalue_access (); + } + } } public override void emit (CodeGenerator codegen) {