public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "adobriyan at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/113124] New: g++ should relax designated initialiser rules for trivial classes (read: C structures) and C arrays.
Date: Sat, 23 Dec 2023 16:17:21 +0000	[thread overview]
Message-ID: <bug-113124-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2023-12-23 16:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-23 16:17 adobriyan at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113124-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).