After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 772430 - Instance cast macros generated by G_DECLARE_* macros don't work with const pointers
Instance cast macros generated by G_DECLARE_* macros don't work with const po...
Status: RESOLVED DUPLICATE of bug 745068
Product: glib
Classification: Platform
Component: gobject
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2016-10-04 21:32 UTC by Razvan Chitu
Modified: 2016-10-04 21:55 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Razvan Chitu 2016-10-04 21:32:59 UTC
The G_DECLARE_* macros generate the instance cast macro as an inline function that takes a gpointer parameter. If the supplied parameter is gconstpointer, the compiler will warn about the const qualifier being dropped.
Comment 1 Emmanuele Bassi (:ebassi) 2016-10-04 21:39:41 UTC
Known issue — duplicate of bug 745068.

Passing const pointers to the various type check macros is not recommended and not something that happens in idiomatic GObject code; most accessors may very well end up not being const functions that do not modify their input — simply acquiring a temporary reference, or emitting a signal would undo that.

The recommendation is: do not use const pointers with GObject instances.

*** This bug has been marked as a duplicate of bug 745068 ***
Comment 2 Carlos Soriano 2016-10-04 21:47:46 UTC
> Passing const pointers to the various type check macros is not recommended
> and not something that happens in idiomatic GObject code; most accessors may

I believe GCompareFunc (and maybe others) should be modified to not use gconstpointers so casts to the arguments work as expected in the callback then?

Also I was taught early that idiomatic gobject/c code was to use const when the function doesn't modify the actual parameters, not the other way around. Is that not the case, or did I misunderstood something?
Comment 3 Carlos Soriano 2016-10-04 21:50:25 UTC
In case it makes more clear our use case, this is the function that made us report this bug report:
https://git.gnome.org/browse/nautilus/tree/src/nautilus-shell-search-provider.c?id=335eabec52e26406c339a9992c432efe28f8ac5e#n177
Comment 4 Emmanuele Bassi (:ebassi) 2016-10-04 21:54:40 UTC
(In reply to Carlos Soriano from comment #2)
> > Passing const pointers to the various type check macros is not recommended
> > and not something that happens in idiomatic GObject code; most accessors may
> 
> I believe GCompareFunc (and maybe others) should be modified to not use
> gconstpointers so casts to the arguments work as expected in the callback
> then?

Not really. Sorting objects is not commonly done.

In any case, you can "undo" a `const`, since this is C, on a case by case basis. Making all type checking functions take a const pointer so long after the fact (and potentially for nothing) is going to break more idiomatic code.

> Also I was taught early that idiomatic gobject/c code was to use const when
> the function doesn't modify the actual parameters, not the other way around.
> Is that not the case, or did I misunderstood something?

That is *generally* the case for Plain Old Structures, or pointers to arrays of characters.

For GObject, having a:

   bool foo_object_is_bar (const FooObject *self);

is decidedly *not* idiomatic — in the sense that nobody does it.
Comment 5 Emmanuele Bassi (:ebassi) 2016-10-04 21:55:49 UTC
(In reply to Carlos Soriano from comment #3)
> In case it makes more clear our use case, this is the function that made us
> report this bug report:
> https://git.gnome.org/browse/nautilus/tree/src/nautilus-shell-search-
> provider.c?id=335eabec52e26406c339a9992c432efe28f8ac5e#n177

A simple fix would be:

-    hit_a = NAUTILUS_SEARCH_HIT (a);
-    hit_b = NAUTILUS_SEARCH_HIT (b);
+    hit_a = (NautilusSearchHit *) a;
+    hit_b = (NautilusSearchHit *) b;

since you control the input of the function.