[vala/0.34] codegen: Don't transfer ownership of local-variable if target-type is unknown



commit bc9599ddc33fdc0abe9c03547b2146a76a24fa3e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Aug 17 08:12:57 2017 +0200

    codegen: Don't transfer ownership of local-variable if target-type is unknown
    
    https://bugzilla.gnome.org/show_bug.cgi?id=736774

 tests/Makefile.am                   |    2 ++
 tests/control-flow/bug736774-1.vala |   23 +++++++++++++++++++++++
 tests/control-flow/bug736774-2.vala |   16 ++++++++++++++++
 vala/valasemanticanalyzer.vala      |    2 +-
 4 files changed, 42 insertions(+), 1 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5518b2d..6e1b5bf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -87,6 +87,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 0000000..1fd70a4
--- /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 0000000..f54ce5c
--- /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 5a83c9b..563ce0e 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -890,7 +890,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 ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]