Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
codegen: Always assign original variable when consuming instance to d…
  • Loading branch information
ricotz committed Apr 8, 2019
1 parent d7443e7 commit 3d83f31
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
15 changes: 6 additions & 9 deletions codegen/valaccodebasemodule.vala
Expand Up @@ -4067,15 +4067,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) {
Expand All @@ -4093,6 +4085,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;
}
}

Expand Down
83 changes: 82 additions & 1 deletion tests/objects/compact-class-destructor.vala
Expand Up @@ -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 ();
}
7 changes: 7 additions & 0 deletions vala/valamemberaccess.vala
Expand Up @@ -906,6 +906,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) {
Expand Down

0 comments on commit 3d83f31

Please sign in to comment.