From: Rico Tzschichholz Date: Sun, 10 May 2020 06:46:33 +0000 (+0200) Subject: codegen: Correctly handle cast-expression of real struct to nullable struct X-Git-Tag: 0.49.1~136 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c49d45a4e52d5a572ca6e10ecdd5c475eb768720;p=thirdparty%2Fvala.git codegen: Correctly handle cast-expression of real struct to nullable struct Don't generate invalid c-code leading to "cannot convert to a pointer type" Fix https://gitlab.gnome.org/GNOME/vala/issues/991 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index fb3c70154..b4a65dd77 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -5342,6 +5342,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { expr.inner.value_type is ValueType && expr.inner.value_type.nullable) { // nullable integer or float or boolean or struct or enum cast to non-nullable innercexpr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, innercexpr); + } else if (expr.type_reference is ValueType && expr.type_reference.nullable && + expr.inner.value_type.is_real_non_null_struct_type ()) { + // real non-null struct cast to nullable + innercexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, innercexpr); } else if (expr.type_reference is ArrayType && !(expr.inner is Literal) && expr.inner.value_type is ValueType && !expr.inner.value_type.nullable) { // integer or float or boolean or struct or enum to array cast diff --git a/tests/Makefile.am b/tests/Makefile.am index d5038fd9d..7a99b0af2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -267,6 +267,7 @@ TESTS = \ enums/bug780050.vala \ structs/struct_only.vala \ structs/struct-base-types.vala \ + structs/struct-boxed-cast.vala \ structs/struct-empty-still.test \ structs/struct-initializer-list-in-array.vala \ structs/struct-no-gtype.vala \ diff --git a/tests/structs/struct-boxed-cast.vala b/tests/structs/struct-boxed-cast.vala new file mode 100644 index 000000000..260819dcf --- /dev/null +++ b/tests/structs/struct-boxed-cast.vala @@ -0,0 +1,21 @@ +void foo (T t) { + assert (((Bar?) t).s == "foo"); + assert (((Bar?) t).i == 23); +} + +struct Bar { + public string s; + public int i; +} + +void main () { + Bar f = { "bar", 42 }; + var cast = (Bar?) f; + assert (cast.s == "bar"); + assert (cast.i == 42); + + Bar arg = { "foo", 23 }; + foo ((Bar?) arg); + foo (arg); + foo ((Bar?) arg); +}