]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Correctly handle cast-expression of real struct to nullable struct
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 10 May 2020 06:46:33 +0000 (08:46 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 10 May 2020 10:56:12 +0000 (12:56 +0200)
Don't generate invalid c-code leading to "cannot convert to a pointer type"

Fix https://gitlab.gnome.org/GNOME/vala/issues/991

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/struct-boxed-cast.vala [new file with mode: 0644]

index fb3c70154d71a771318660340c60d40fdefb6726..b4a65dd7741f31f08ccc02e70b1b8aae74c272bb 100644 (file)
@@ -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
index d5038fd9d8537eba8343a2c04f5a4e0a6985476c..7a99b0af2464e57e3b292e1111410d8173794593 100644 (file)
@@ -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 (file)
index 0000000..260819d
--- /dev/null
@@ -0,0 +1,21 @@
+void foo<T> (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<Bar?> (arg);
+       foo<Bar?> ((Bar?) arg);
+}