[vala/staging] codegen: Fix passing non-nullable structs to nullable parameters



commit 98b1072dc35366c7dc9a5f00e93b6caf04d2172f
Author: Luca Bruno <lucabru src gnome org>
Date:   Wed Nov 28 20:43:56 2012 +0100

    codegen: Fix passing non-nullable structs to nullable parameters
    
    Fixes bug 685177.

 codegen/valaccodebasemodule.vala |    7 ++++++-
 tests/Makefile.am                |    1 +
 tests/structs/bug685177.vala     |   35 +++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 03f801a..f84ed67 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4829,7 +4829,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			var glib_value = (GLibValue) expr.inner.target_value;
 
 			var ref_value = new GLibValue (glib_value.value_type);
-			ref_value.cvalue = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, glib_value.cvalue);
+			if (expr.target_type != null && glib_value.value_type.is_real_struct_type () && glib_value.value_type.nullable != expr.target_type.nullable) {
+				// the only possibility is that value_type is nullable and target_type is non-nullable
+				ref_value.cvalue = glib_value.cvalue;
+			} else {
+				ref_value.cvalue = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, glib_value.cvalue);
+			}
 
 			if (glib_value.array_length_cvalues != null) {
 				for (int i = 0; i < glib_value.array_length_cvalues.size; i++) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3d2bc4a..93dc65d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -87,6 +87,7 @@ TESTS = \
 	structs/bug661945.vala \
 	structs/bug667890.vala \
 	structs/bug669580.vala \
+	structs/bug685177.vala \
 	delegates/delegates.vala \
 	delegates/bug539166.vala \
 	delegates/bug595610.vala \
diff --git a/tests/structs/bug685177.vala b/tests/structs/bug685177.vala
new file mode 100644
index 0000000..6aa3060
--- /dev/null
+++ b/tests/structs/bug685177.vala
@@ -0,0 +1,35 @@
+void non_nullable (Value v1, ref Value v2, out Value v3) {
+	v3 = v2;
+	v2 = v1;
+}
+
+void nullable (Value? v1, ref Value? v2, out Value? v3) {
+	v3 = v2;
+	v2 = null;
+}
+
+void main () {
+	Value v1 = 1;
+	Value v2 = 2;
+	Value v3;
+	non_nullable (v1, ref v2, out v3);
+	assert ((int)v1 == 1);
+	assert ((int)v2 == 1);
+	assert ((int)v3 == 2);
+
+	Value? v4 = 4;
+	Value? v5 = 5;
+	Value? v6 = 6;
+	non_nullable (v4, ref v5, out v6);
+	assert ((int)v4 == 4);
+	assert ((int)v5 == 4);
+	assert ((int)v6 == 5);
+
+	v4 = 4;
+	v5 = 5;
+	v6 = 6;
+	nullable (v4, ref v5, out v6);
+	assert ((int)v4 == 4);
+	assert (v5 == null);
+	assert ((int)v6 == 5);
+}



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