Void cast removes the function call
Problem: vala removes function calls when their result is cast into void
I found this problem when I was trying to cast the function result into void. In C, some people cast function results into void when to indicate when they ignore the return result on purpose (For side-effect only). I tried to do the same thing in Vala but for some reason casting the return value into void results with removing the function call. I think this can be optimization for non-side-effect functions but for functions that have side effects they shouldn't be removed.
Example code
public static void main (string[] args) {
(void) foo (); // cast into void to indicate that return value is ignored
}
int foo () {
print ("called foo\n");
return 0;
}
C output:
/* main.c generated by valac 0.48.10, the Vala compiler
* generated from main.vala, do not modify */
#include <stdlib.h>
#include <string.h>
#include <glib.h>
void _vala_main (gchar** args,
gint args_length1);
gint foo (void);
void
_vala_main (gchar** args,
gint args_length1)
{
// should call foo here!
}
int
main (int argc,
char ** argv)
{
_vala_main (argv, argc);
return 0;
}
gint
foo (void)
{
gint result = 0;
g_print ("called foo\n");
result = 0;
return result;
}
Program output:
Expected output:
called foo
The problem is _vala_main
is empty while it should call foo
function. The workaround is to simply not cast into void. But I think this is serious problem since compiler gives no warnings and silently removes the function call while user probably expects it to run.