Discards ‘volatile’ qualifier warning in Vala

Hi,

I have this very simple code:

example.catalog.vala

namespace Example {

    public class Catalog : Object {

        public Catalog (string dir) {
        }

        public string catalog_dir { get; }
    }

}

And here is where the class above gets instantiated:

example.vala

namespace Example {

    public Example.Catalog ex;

    public static int main () {
        var ex = new Example.Catalog (".");

        print ("%s\n", ex.catalog_dir);

        return 0;
    }

}

After I compile this code with:
$ valac -o example example.vala example.catalog.vala

I get these warnings:

In file included from /usr/include/glib-2.0/glib/gthread.h:34,
                 from /usr/include/glib-2.0/glib/gasyncqueue.h:34,
                 from /usr/include/glib-2.0/glib.h:34,
                 from /usr/include/glib-2.0/gobject/gbinding.h:30,
                 from /usr/include/glib-2.0/glib-object.h:24,
                 from /.../example/example.catalog.vala.c:4:
/.../example/example.catalog.vala.c: In function ‘example_catalog_get_type’:
/usr/include/glib-2.0/glib/gatomic.h:131:5: warning: argument 2 of ‘__atomic_load’ discards ‘volatile’ qualifier [-Wincompatible-pointer-types]
  131 |     __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
      |     ^~~~~~~~~~~~~
/usr/include/glib-2.0/glib/gthread.h:262:7: note: in expansion of macro ‘g_atomic_pointer_get’
  262 |     (!g_atomic_pointer_get (location) &&                             \
      |       ^~~~~~~~~~~~~~~~~~~~
/.../example/example.catalog.vala.c:140:13: note: in expansion of macro ‘g_once_init_enter’
  140 |         if (g_once_init_enter (&example_catalog_type_id__once)) {
      |             ^~~~~~~~~~~~~~~~~

As a note, I replaced the path to the source code with “…”.

Then, hoping to get some clues about what the issue might be, I go even further and compile the C code from Vala:

$ valac -C example.vala example.catalog.vala
$ gcc `pkg-config --cflags glib-2.0` example.c example.catalog.c `pkg-config --libs glib-2.0`

And, as expected, I get some warnings here as well, and more:

In file included from /usr/include/glib-2.0/glib/gthread.h:34,
                 from /usr/include/glib-2.0/glib/gasyncqueue.h:34,
                 from /usr/include/glib-2.0/glib.h:34,
                 from /usr/include/glib-2.0/gobject/gbinding.h:30,
                 from /usr/include/glib-2.0/glib-object.h:24,
                 from example.catalog.c:4:
example.catalog.c: In function ‘example_catalog_get_type’:
/usr/include/glib-2.0/glib/gatomic.h:131:5: warning: argument 2 of ‘__atomic_load’ discards ‘volatile’ qualifier [-Wincompatible-pointer-types]
  131 |     __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
      |     ^~~~~~~~~~~~~
/usr/include/glib-2.0/glib/gthread.h:262:7: note: in expansion of macro ‘g_atomic_pointer_get’
  262 |     (!g_atomic_pointer_get (location) &&                             \
      |       ^~~~~~~~~~~~~~~~~~~~
example.catalog.c:140:13: note: in expansion of macro ‘g_once_init_enter’
  140 |         if (g_once_init_enter (&example_catalog_type_id__once)) {
      |             ^~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccJCS04B.o: in function `example_main':
example.c:(.text+0x74): undefined reference to `g_object_unref'
/usr/bin/ld: /tmp/ccaVzm6P.o: in function `example_catalog_construct':
example.catalog.c:(.text+0x70): undefined reference to `g_object_new'
/usr/bin/ld: /tmp/ccaVzm6P.o: in function `example_catalog_class_init':
example.catalog.c:(.text+0x111): undefined reference to `g_type_class_peek_parent'
/usr/bin/ld: example.catalog.c:(.text+0x12e): undefined reference to `g_type_class_adjust_private_offset'
/usr/bin/ld: example.catalog.c:(.text+0x13f): undefined reference to `g_type_check_class_cast'
/usr/bin/ld: example.catalog.c:(.text+0x15b): undefined reference to `g_type_check_class_cast'
/usr/bin/ld: example.catalog.c:(.text+0x194): undefined reference to `g_param_spec_string'
/usr/bin/ld: example.catalog.c:(.text+0x1b3): undefined reference to `g_type_check_class_cast'
/usr/bin/ld: example.catalog.c:(.text+0x1c3): undefined reference to `g_object_class_install_property'
/usr/bin/ld: /tmp/ccaVzm6P.o: in function `example_catalog_finalize':
example.catalog.c:(.text+0x214): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: example.catalog.c:(.text+0x24e): undefined reference to `g_type_check_class_cast'
/usr/bin/ld: /tmp/ccaVzm6P.o: in function `example_catalog_get_type_once':
example.catalog.c:(.text+0x289): undefined reference to `g_type_register_static'
/usr/bin/ld: example.catalog.c:(.text+0x29e): undefined reference to `g_type_add_instance_private'
/usr/bin/ld: /tmp/ccaVzm6P.o: in function `_vala_example_catalog_get_property':
example.catalog.c:(.text+0x34b): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: example.catalog.c:(.text+0x373): undefined reference to `g_value_set_string'
/usr/bin/ld: example.catalog.c:(.text+0x3a0): undefined reference to `g_type_name'
/usr/bin/ld: example.catalog.c:(.text+0x3b5): undefined reference to `g_type_name'
collect2: error: ld returned 1 exit status

I know I can hide these warning with -X -w, but this is not what I would like to do, unless it’s something I understand and I can confidently ignore.

This is not a singular case and I wonder if someone can explain what they are and how I can remove (not hide) them.

Debian GNU/Linux 12 (bookworm), 43.9, Wayland
Vala 0.56.3

Any help is greatly appreciated.

Thank you

This should include gobject-2.0 as below.

$ gcc $(pkg-config --cflags gobject-2.0) example.c example.catalog.c $(pkg-config --libs gobject-2.0)

The discards 'volatile' qualifier is from g_once_init_enter() in line 148 below.

   144  GType
   145  example_catalog_get_type (void)
   146  {
   147          static volatile gsize example_catalog_type_id__once = 0;
   148          if (g_once_init_enter (&example_catalog_type_id__once)) {
   149                  GType example_catalog_type_id;
   150                  example_catalog_type_id = example_catalog_get_type_once ();
   151                  g_once_init_leave (&example_catalog_type_id__once, example_catalog_type_id);
   152          }
   153          return example_catalog_type_id__once;
   154  }
   155  

This is due to the fact that g_once_init_enter() was a function in the past and used to take a volatile location argument. The volatile qualifier currently is only a historical artifact and should not be used as mentioned in the docs. For more details you can refer:

$ git log --grep=volatile glib/gthread.c

So, changing

   147          static volatile gsize example_catalog_type_id__once = 0;

to

   147          static gsize example_catalog_type_id__once = 0;

in C code should remove the warning, but it’s up to the vala codegen.

So, it should be safe to ignore the warning.

2 Likes

You should always build Vala projects with -w. Otherwise, you’ll never notice actual Vala warnings that you need to fix in the sea of warnings coming from the generated C code.

Good point. Thank you.