]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Don't allow GLib.Value casting to nullable struct/simple types
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 3 Feb 2021 12:40:23 +0000 (13:40 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 3 Feb 2021 13:29:12 +0000 (14:29 +0100)
tests/Makefile.am
tests/semantic/cast-gvalue-unsupported.test [new file with mode: 0644]
vala/valacastexpression.vala

index bc5888bbec45b9893441da7edf192f3ad57d190b..b8b15b2cf652165ae73208c4a4af67610a860723 100644 (file)
@@ -870,6 +870,7 @@ TESTS = \
        semantic/assignment-same-variable.vala \
        semantic/assignment-signal-incompatible-method.test \
        semantic/assignment-signal-incompatible-type.test \
+       semantic/cast-gvalue-unsupported.test \
        semantic/cast-gvariant-unsupported.test \
        semantic/chainup-gobject-incompatible-type-property.test \
        semantic/chainup-gobject-unknown-property.test \
diff --git a/tests/semantic/cast-gvalue-unsupported.test b/tests/semantic/cast-gvalue-unsupported.test
new file mode 100644 (file)
index 0000000..ef60d90
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       Value v = Value (typeof (int));
+       var i = (int?) v;
+}
index cc3dbca9ebf03db3189c3e37a63cc0f34549d58a..9aff511facdf317630004756c1aefd4aaee25983 100644 (file)
@@ -215,6 +215,10 @@ public class Vala.CastExpression : Expression {
                    && is_gvalue (context, inner.value_type) && !is_gvalue (context, value_type)) {
                        // GValue unboxing returns unowned value
                        value_type.value_owned = false;
+                       if (value_type.nullable && value_type.type_symbol != null && !value_type.type_symbol.is_reference_type ()) {
+                               error = true;
+                               Report.error (source_reference, "Casting of `GLib.Value' to `%s' is not supported", value_type.to_qualified_string ());
+                       }
                }
 
                inner.target_type = inner.value_type.copy ();