[vala/staging: 3/4] vala: Don't allow cast-expression used as expression-statement




commit 48d5aa2a24c2a687e64db16f959c4965692d432b
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Sep 29 14:26:41 2020 +0200

    vala: Don't allow cast-expression used as expression-statement
    
    It is not useful to suggest this is works like in C. This behavior was
    never supported and these expressions were skipped completely.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1070

 tests/Makefile.am                    | 1 +
 tests/semantic/cast-unsupported.test | 5 +++++
 vala/valacastexpression.vala         | 6 ++++++
 3 files changed, 12 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index de02d1607..41d4438fa 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -805,6 +805,7 @@ TESTS = \
        semantic/assignment-signal-incompatible-method.test \
        semantic/assignment-signal-incompatible-type.test \
        semantic/cast-gvariant-unsupported.test \
+       semantic/cast-unsupported.test \
        semantic/chainup-gobject-incompatible-type-property.test \
        semantic/chainup-gobject-unknown-property.test \
        semantic/chainup-gobject-unsupported-type-property.test \
diff --git a/tests/semantic/cast-unsupported.test b/tests/semantic/cast-unsupported.test
new file mode 100644
index 000000000..4f35a4417
--- /dev/null
+++ b/tests/semantic/cast-unsupported.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       (void*) "foo";
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index f86aad157..c2c07f36b 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -164,6 +164,12 @@ public class Vala.CastExpression : Expression {
 
                // FIXME: check whether cast is allowed
 
+               if (parent_node is ExpressionStatement) {
+                       Report.error (source_reference, "Cast expression is not supported in this context");
+                       error = true;
+                       return false;
+               }
+
                if (type_reference is DelegateType && inner.value_type is MethodType) {
                        if (target_type != null) {
                                inner.value_type.value_owned = target_type.value_owned;


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