From: Luca Bruno Date: Wed, 28 Nov 2012 19:43:56 +0000 (+0100) Subject: codegen: Fix passing nullable structs to non-nullable parameters X-Git-Tag: 0.19.0~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b482c7c44736c0403bd67acfa92b10062ed1958;p=thirdparty%2Fvala.git codegen: Fix passing nullable structs to non-nullable parameters Fixes bug 685177. --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 03f801a75..f84ed670f 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 3d2bc4a9f..93dc65dbf 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 000000000..6aa306054 --- /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); +}