public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113124] New: g++ should relax designated initialiser rules for trivial classes (read: C structures) and C arrays.
@ 2023-12-23 16:17 adobriyan at gmail dot com
  2023-12-23 16:21 ` [Bug c++/113124] " adobriyan at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: adobriyan at gmail dot com @ 2023-12-23 16:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113124

            Bug ID: 113124
           Summary: g++ should relax designated initialiser rules for
                    trivial classes (read: C structures) and C arrays.
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adobriyan at gmail dot com
  Target Milestone: ---

Currently, the rules do not allow reordering member initialisation:

    struct S {
        int a;
        int b;
    };
    S s{.b = 1, .a = 2}; // error

or skipping array element initialisation:

    int a[2] = {[1] = 1}; // error

First, clang++ allows it with warnings which can be silenced.

Second, both restrictions are major problem for converting C projects to C++
if C99 initialisers are used often.

I have bootable Linux compiled with g++ and it became obvious that some changes
are such pain points:
1) implicit casts from void* to T*,
2) "broken" C99 initializers (which Linux uses a lot),
3) pointer arithmetic ("void* + int" and "void* - void*").

(1) and (3) happen _a_lot_ but they aren't a problem, because they aren't a
flag day, casts can be added little by little to any project willing to convert
to C++.

However, (2) is a flag day with g++, suddenly lot of stuff won't compile.
Fixing this requires giving up _nice_ C99 feature which people are used to.
As you all know, reodering struct members doesn't force to change all
initialisers.

But at the moment of doing gcc => g++ flip every single structure becomes
trivial class(?) and restrictions apply.

Linux is _full_ of these misordered initialisations:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/block/fops.c?h=v6.7-rc6#n838
(the order is .llseek, .read_iter, ...)

Arrays can be even more painful:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/mem.c?h=v6.7-rc6#n685

Note, that the last element is undef ifdef, so less space is wasted,
so giving this array size requires constexpr variable and duplicating ifdefs.

Sometimes it is not obvious which elements are reordeded or missing:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ethtool/common.c?h=v6.7-rc6#n11

Reordered C99 initialisers create problems with maintaining patchset.
Reapplying on top of new version may create some rejects, which are painful for
a human to fix: members can be added/deleted/reorders in main code and then
patch is reordering some members too, so duplicate initializations can appear
if done wrong,
or more importantly, some initializers can be lost (which is null function
pointer dereference somewhere). I've started to redo every initialisers from
scratch just to minimise about of breakage, otherwise it is very easy to miss
stuff.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-01-11 22:22 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-23 16:17 [Bug c++/113124] New: g++ should relax designated initialiser rules for trivial classes (read: C structures) and C arrays adobriyan at gmail dot com
2023-12-23 16:21 ` [Bug c++/113124] " adobriyan at gmail dot com
2023-12-23 16:22 ` adobriyan at gmail dot com
2023-12-23 17:12 ` pinskia at gcc dot gnu.org
2023-12-23 17:15 ` pinskia at gcc dot gnu.org
2023-12-23 17:45 ` pinskia at gcc dot gnu.org
2023-12-27 16:49 ` adobriyan at gmail dot com
2023-12-28 10:32 ` xry111 at gcc dot gnu.org
2023-12-28 11:40 ` adobriyan at gmail dot com
2023-12-28 12:32 ` xry111 at gcc dot gnu.org
2024-01-11 22:22 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).