[vala] Fix struct initialization when temporary variables are used



commit c1ee79af32db6572ab1cd3d910d30c10cfbd95cb
Author: Jürg Billeter <j bitron ch>
Date:   Sun Mar 21 18:35:42 2010 +0100

    Fix struct initialization when temporary variables are used
    
    Fixes bug 613513.

 codegen/valaccodebasemodule.vala |   33 ++++++++++++++++++++++-----------
 tests/Makefile.am                |    1 +
 tests/structs/bug613513.vala     |   10 ++++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e68e889..ded33db 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3019,16 +3019,25 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			return;
 		}
 
-		var expr_type = expr.value_type;
-		if (expr.target_type != null) {
-			expr_type = expr.target_type;
-		}
+		var expr_list = new CCodeCommaExpression ();
 
-		var full_expr_var = get_temp_variable (expr_type, true, expr, false);
-		expr.temp_vars.add (full_expr_var);
+		LocalVariable full_expr_var = null;
+
+		var local_decl = expr.parent_node as LocalVariable;
+		var st = local_decl != null ? local_decl.variable_type.data_type as Struct : null;
+		if (st != null && !st.is_simple_type () && !local_decl.variable_type.nullable) {
+			expr_list.append_expression ((CCodeExpression) expr.ccodenode);
+		} else {
+			var expr_type = expr.value_type;
+			if (expr.target_type != null) {
+				expr_type = expr.target_type;
+			}
+
+			full_expr_var = get_temp_variable (expr_type, true, expr, false);
+			expr.temp_vars.add (full_expr_var);
 		
-		var expr_list = new CCodeCommaExpression ();
-		expr_list.append_expression (new CCodeAssignment (get_variable_cexpression (full_expr_var.name), (CCodeExpression) expr.ccodenode));
+			expr_list.append_expression (new CCodeAssignment (get_variable_cexpression (full_expr_var.name), (CCodeExpression) expr.ccodenode));
+		}
 		
 		foreach (LocalVariable local in temp_ref_vars) {
 			var ma = new MemberAccess.simple (local.name);
@@ -3036,9 +3045,11 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			ma.value_type = local.variable_type.copy ();
 			expr_list.append_expression (get_unref_expression (get_variable_cexpression (local.name), local.variable_type, ma));
 		}
-		
-		expr_list.append_expression (get_variable_cexpression (full_expr_var.name));
-		
+
+		if (full_expr_var != null) {
+			expr_list.append_expression (get_variable_cexpression (full_expr_var.name));
+		}
+
 		expr.ccodenode = expr_list;
 		
 		temp_ref_vars.clear ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b746e19..a71eda7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -45,6 +45,7 @@ TESTS = \
 	structs/bug583603.vala \
 	structs/bug595587.vala \
 	structs/bug606202.vala \
+	structs/bug613513.vala \
 	delegates/delegates.vala \
 	delegates/bug595610.vala \
 	delegates/bug595639.vala \
diff --git a/tests/structs/bug613513.vala b/tests/structs/bug613513.vala
new file mode 100644
index 0000000..3140c6f
--- /dev/null
+++ b/tests/structs/bug613513.vala
@@ -0,0 +1,10 @@
+struct Foo {
+	int i;
+
+	public Foo (string s) {
+	}
+}
+
+void main () {
+	var foo = Foo ("hello" + 42.to_string ());
+}



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