Search code examples
unicodeutf-8vala

Why am I getting a compilation warning when converting a unichar to a string in vala?


Here is a short program to demonstrate the issue:

void main(string[] args)
{
    unichar c = 'a';
    string str_from_to_string = c.to_string(); // Warning
    stdout.printf("Converted by unichar.to_string: \"%s\"\n", str_from_to_string);
}

This also causes the same warning:

void main(string[] args)
{
    unichar c = 'a';
    string str_from_template = @"$c"; // Warning
    stdout.printf("Converted by string template: \"%s\"\n", str_from_template);
}

This is the warning that I get:

/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c: In function ‘g_unichar_to_string’:
/home/mpiroc/Desktop/unicode_to_string/unicode_to_string.vala.c:26:2: warning: passing argument 2 of ‘g_unichar_to_utf8’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/include/glib-2.0/glib/gunicode.h:684:11: note: expected ‘gchar *’ but argument is of type ‘const gchar *’

And here is the generated c code mentioned in the warning:

 18 static gchar* g_unichar_to_string (gunichar self) {
 19     gchar* result = NULL;
 20     gchar* _tmp0_ = NULL;
 21     gchar* str;
 22     const gchar* _tmp1_;
 23     _tmp0_ = g_new0 (gchar, 7);
 24     str = (gchar*) _tmp0_;
 25     _tmp1_ = str;
 26     g_unichar_to_utf8 (self, _tmp1_);
 27     result = str;
 28     return result;
 29 }

It seems as though _tmp_ probably should not be marked const, but this is generated by valac, not written directly by me. Am I doing something wrong? Or is this a bug in valac? The code functions as expected, but I try to avoid warnings when possible.


Solution

  • Add --disable-warnings command-line option

    IIRC Vala compiler can also use --disable-warnigs command-line option.

    Edit:

    gcc commandline options

    Sorry, Valac is transpiler, so once valac --ccode output, and then need to run gcc or another compiler.

    $ valac --ccode unicode_to_string.vala
    $ gcc -o unicode_to_string -w unicode_to_string.c `pkg-config --libs --cflags gobject-2.0`