[vala/0.52] vala: Warn about unsupported cast to void and drop it



commit fd1811e22f7ea417530ed143b804b62900124ce8
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Sep 14 23:28:43 2021 +0200

    vala: Warn about unsupported cast to void and drop it
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1070

 tests/Makefile.am                                  |  2 +-
 tests/semantic/cast-void-not-allowed.vala          | 11 +++++++++++
 tests/semantic/localvariable-void-initializer.test |  5 -----
 vala/valacastexpression.vala                       |  7 +++++++
 4 files changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7c04805a9..3671f3312 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -933,6 +933,7 @@ TESTS = \
        semantic/assignment-signal-incompatible-type.test \
        semantic/cast-gvalue-unsupported.test \
        semantic/cast-gvariant-unsupported.test \
+       semantic/cast-void-not-allowed.vala \
        semantic/chainup-gobject-incompatible-type-property.test \
        semantic/chainup-gobject-unknown-property.test \
        semantic/chainup-gobject-unsupported-type-property.test \
@@ -1028,7 +1029,6 @@ TESTS = \
        semantic/localvariable-var-static-access-instance-property.test \
        semantic/localvariable-var-without-initializer.test \
        semantic/localvariable-void.test \
-       semantic/localvariable-void-initializer.test \
        semantic/member-access-async-callback-invalid.test \
        semantic/member-access-capture-out.test \
        semantic/member-access-capture-valist-parameter.test \
diff --git a/tests/semantic/cast-void-not-allowed.vala b/tests/semantic/cast-void-not-allowed.vala
new file mode 100644
index 000000000..dee3801de
--- /dev/null
+++ b/tests/semantic/cast-void-not-allowed.vala
@@ -0,0 +1,11 @@
+bool success = false;
+
+bool foo () {
+       success = true;
+       return true;
+}
+
+void main () {
+       (void) foo ();
+       assert (success);
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index 9aff511fa..1d1dbb0f2 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -164,6 +164,13 @@ public class Vala.CastExpression : Expression {
 
                // FIXME: check whether cast is allowed
 
+               if (type_reference is VoidType) {
+                       Report.warning (source_reference, "Casting to `void' is not supported");
+                       context.analyzer.replaced_nodes.add (this);
+                       parent_node.replace_expression (this, inner);
+                       return inner.check (context);
+               }
+
                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]