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



commit 37ca3450755542a1f04d177d94a18b792ba970e6
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 0d6a54d1e..e979286d9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -842,6 +842,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 bce686c99..263938dd8 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".printf (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]