]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Warn about unsupported cast to void and drop it
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 14 Sep 2021 21:28:43 +0000 (23:28 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 23 Sep 2021 08:13:47 +0000 (10:13 +0200)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1070

tests/Makefile.am
tests/semantic/cast-void-not-allowed.vala [new file with mode: 0644]
tests/semantic/localvariable-void-initializer.test [deleted file]
vala/valacastexpression.vala

index 7b4c8f39b6e3d8ae7761b5a849ef9a58ec7c1f5a..83dbff3b5249153c6b622670fd09fdcb133dadc7 100644 (file)
@@ -900,6 +900,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 \
@@ -990,7 +991,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 (file)
index 0000000..dee3801
--- /dev/null
@@ -0,0 +1,11 @@
+bool success = false;
+
+bool foo () {
+       success = true;
+       return true;
+}
+
+void main () {
+       (void) foo ();
+       assert (success);
+}
diff --git a/tests/semantic/localvariable-void-initializer.test b/tests/semantic/localvariable-void-initializer.test
deleted file mode 100644 (file)
index fbfbbff..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-Invalid Code
-
-void main () {
-       var foo = (void) "foo";
-}
index 263938dd80c0526fa7183577e32823f14d49c0e0..de5aa5db3f95c15981e371e68bd43ff403351449 100644 (file)
@@ -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;