From: Rico Tzschichholz Date: Thu, 17 Aug 2017 06:12:57 +0000 (+0200) Subject: codegen: Don't transfer ownership of local-variable if target-type is unknown X-Git-Tag: 0.37.91~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d867edad99ed9396527078fc8b702109b909e0f;p=thirdparty%2Fvala.git codegen: Don't transfer ownership of local-variable if target-type is unknown https://bugzilla.gnome.org/show_bug.cgi?id=736774 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 9baab1c91..93d0a0a61 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -110,6 +110,8 @@ TESTS = \ control-flow/bug661985.vala \ control-flow/bug665904.vala \ control-flow/bug691514.vala \ + control-flow/bug736774-1.vala \ + control-flow/bug736774-2.vala \ enums/enum_only.vala \ enums/enums.vala \ enums/flags.vala \ diff --git a/tests/control-flow/bug736774-1.vala b/tests/control-flow/bug736774-1.vala new file mode 100644 index 000000000..1fd70a40e --- /dev/null +++ b/tests/control-flow/bug736774-1.vala @@ -0,0 +1,23 @@ +bool success = false; + +class Foo : Object { + ~Foo() { + success = true; + } +} + +Foo may_fail () throws Error { + return new Foo (); +} + +void func (Foo foo) { +} + +void main() { + try { + func (may_fail ()); + } catch { + } + + assert (success); +} diff --git a/tests/control-flow/bug736774-2.vala b/tests/control-flow/bug736774-2.vala new file mode 100644 index 000000000..f54ce5cbd --- /dev/null +++ b/tests/control-flow/bug736774-2.vala @@ -0,0 +1,16 @@ +string* keep; + +string may_fail () throws GLib.Error { + string result = "test"; + keep = result; + return (owned) result; +} + +void main () { + try { + print (_("%s\n"), may_fail ()); + } catch { + } + + assert (keep != "test"); +} diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala index f3b1a633d..44b75f968 100644 --- a/vala/valasemanticanalyzer.vala +++ b/vala/valasemanticanalyzer.vala @@ -895,7 +895,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor { public static Expression create_temp_access (LocalVariable local, DataType? target_type) { Expression temp_access = new MemberAccess.simple (local.name, local.source_reference); - var target_owned = target_type == null || target_type.value_owned; + var target_owned = target_type != null && target_type.value_owned; if (target_owned && local.variable_type.is_disposable ()) { temp_access = new ReferenceTransferExpression (temp_access, local.source_reference); temp_access.target_type = target_type != null ? target_type.copy () : local.variable_type.copy ();