[vala/staging: 1/2] vala: Warn about unsupported cast to void and drop it




commit f00ab1c7905b70280fc85df6efc4a2dd3f8bc141
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.c-expected    | 50 ++++++++++++++++++++++
 tests/semantic/cast-void-not-allowed.vala          | 11 +++++
 tests/semantic/localvariable-void-initializer.test |  5 ---
 vala/valacastexpression.vala                       |  7 +++
 5 files changed, 69 insertions(+), 6 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ec9c97b13..d84703e65 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -948,6 +948,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 \
@@ -1043,7 +1044,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.c-expected b/tests/semantic/cast-void-not-allowed.c-expected
new file mode 100644
index 000000000..93f91458f
--- /dev/null
+++ b/tests/semantic/cast-void-not-allowed.c-expected
@@ -0,0 +1,50 @@
+/* semantic_cast_void_not_allowed.c generated by valac, the Vala compiler
+ * generated from semantic_cast_void_not_allowed.vala, do not modify */
+
+#include <glib.h>
+
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, 
G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning 
(G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+
+VALA_EXTERN gboolean success;
+gboolean success = FALSE;
+
+VALA_EXTERN gboolean foo (void);
+static void _vala_main (void);
+
+gboolean
+foo (void)
+{
+       gboolean result = FALSE;
+       success = TRUE;
+       result = TRUE;
+       return result;
+}
+
+static void
+_vala_main (void)
+{
+       foo ();
+       _vala_assert (success, "success");
+}
+
+int
+main (int argc,
+      char ** argv)
+{
+       _vala_main ();
+       return 0;
+}
+
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]