[vala] Avoid unnecessary copies when using the coalescing operator



commit 19184595677fdd08acf35c32ff78a3d97faf3ab2
Author: Luca Bruno <lucabru src gnome org>
Date:   Sun Jan 26 15:49:18 2014 +0100

    Avoid unnecessary copies when using the coalescing operator
    
    Fixes bug 661985

 tests/Makefile.am                 |    1 +
 tests/control-flow/bug661985.vala |    7 +++++++
 vala/valabinaryexpression.vala    |   22 +++++++++-------------
 3 files changed, 17 insertions(+), 13 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5d89abf..f8f882c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -65,6 +65,7 @@ TESTS = \
        control-flow/sideeffects.vala \
        control-flow/bug639482.vala \
        control-flow/bug652549.vala \
+       control-flow/bug661985.vala \
        control-flow/bug665904.vala \
        control-flow/bug691514.vala     \
        enums/enums.vala \
diff --git a/tests/control-flow/bug661985.vala b/tests/control-flow/bug661985.vala
new file mode 100644
index 0000000..649af7d
--- /dev/null
+++ b/tests/control-flow/bug661985.vala
@@ -0,0 +1,7 @@
+void main () {
+       string foo = "foo";
+       void* foop = foo;
+       unowned string bar = foo ?? "bar";
+       void* barp = bar;
+       assert (foop == barp);
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index 19766e1..9c1ef4c 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -194,7 +194,12 @@ public class Vala.BinaryExpression : Expression {
                }
 
                if (operator == BinaryOperator.COALESCE) {
-                       var local = new LocalVariable (null, get_temp_name (), left, source_reference);
+                       if (!left.check (context)) {
+                               error = true;
+                               return false;
+                       }
+                       
+                       var local = new LocalVariable (left.value_type != null ? left.value_type.copy () : 
null, get_temp_name (), left, source_reference);
                        var decl = new DeclarationStatement (local, source_reference);
 
                        var right_stmt = new ExpressionStatement (new Assignment (new MemberAccess.simple 
(local.name, right.source_reference), right, AssignmentOperator.SIMPLE, right.source_reference), 
right.source_reference);
@@ -220,19 +225,10 @@ public class Vala.BinaryExpression : Expression {
                                return false;
                        }
 
-                       var ma = new MemberAccess.simple (local.name, source_reference);
-                       Expression replace = ma;
-
-                       if (target_type == null) {
-                               replace = new ReferenceTransferExpression (replace, source_reference);
-                               replace.target_type = local.variable_type.copy ();
-                               replace.target_type.value_owned = true;
-                       } else {
-                               replace.target_type = target_type.copy ();
-                       }
-                       replace.check (context);
+                       var temp_access = SemanticAnalyzer.create_temp_access (local, target_type);
+                       temp_access.check (context);
 
-                       parent_node.replace_expression (this, replace);
+                       parent_node.replace_expression (this, temp_access);
 
                        return true;
                }


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