[vala/staging: 3/4] vala: Don't allow GLib.Value casting to nullable struct/simple types




commit 4d023541e4b013a05d55a258044b6a20e6fb7e01
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Feb 3 13:40:23 2021 +0100

    vala: Don't allow GLib.Value casting to nullable struct/simple types

 tests/Makefile.am                           | 1 +
 tests/semantic/cast-gvalue-unsupported.test | 6 ++++++
 vala/valacastexpression.vala                | 4 ++++
 3 files changed, 11 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bc5888bbe..b8b15b2cf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..ef60d90ca
--- /dev/null
+++ b/tests/semantic/cast-gvalue-unsupported.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       Value v = Value (typeof (int));
+       var i = (int?) v;
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index cc3dbca9e..9aff511fa 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -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 ();


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