Re: [PATCH] CodingGuidelines: allow compound literals
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Oct 27, 2026, 06:36 UTC
- Message-ID
- <auBGiu_-qJMd07ua@pks.im>
- In-Reply-To
- <xmqqbj8gp3pv.fsf@gitster.g>
On Mon, Oct 26, 2026 at 02:11:08PM -0700, Junio C Hamano wrote:
Show 19 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes: > > > From: Phillip Wood <phillip.wood@dunelm.org.uk> > > > > Our coding guidelines say that we'll allow compound literals from > > mid 2026 if no problems are reported in relation to the test balloon > > added in 01e49941d6 (reftable/system: provide thin wrapper for tempfile > > subsystem, 2024-11-18). As no such reports have been received, update > > the guidelines to allow this construct. > > > > Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> > > --- > > Yeah, I agree that it is about time to promote this to the accepted > set. > > So we do not have test balloon but we know we still cannot use two > constructs from C99 due to MinGW and older IBM xlc? Perhaps we can > raise test balloons for them by now?
Huh, which are those two constructs?
This also makes me wonder whether we should have a look at what C11 has to offer. Skimming through it though the only thing that I do find to be interesting is `_static_assert()`. We essentially already have those, but they are extremely hacky and their error messages leave a lot to be desired.
C17 is rather uninteresting, and the next version that provides some significant value is C23. But that one is probably way too fancy and new to be a sensible target for us now.
> Will merge and mark the topic as ready for 'next'. Thanks.
Yup, agreed.
One thing I wonder is whether we can now start to replace the split between e.g. `STRBUF_INIT` and `strbuf_init()`. Right now the issue here is that you cannot initialize a variable at a later point like this:
struct strbuf buf;
buf = STRBUF_INIT;
This is because `STRBUF_INIT` is simply defined as `{ .buf = ... }`, and thus the compiler would complain. So instead, you are forced to now call `strbuf_init()` on that variable.
But with compound literals we can define it as `#define STRBUF_INIT ((struct strbuf) { .buf = ... }`, which would make the above code to compile just fine. And as a consequence, `strbuf_init()` essentially becomes obsolete.
Now I'm of course not proposing to go on a crusade and replace every such site, and I only use `strbuf` as an example here. But is that a direction we want to move towards?
Patrick