]> git.ipfire.org Git - thirdparty/gcc.git/commit
c: Give permerror for excess braces in scalar initializers [PR88642]
authorJoseph Myers <josmyers@redhat.com>
Fri, 26 Sep 2025 11:12:12 +0000 (11:12 +0000)
committerJoseph Myers <josmyers@redhat.com>
Fri, 26 Sep 2025 11:12:12 +0000 (11:12 +0000)
commit1b876bdffd875ce061b35c7bb73f76a9bf7ec9f2
treea3883968e580361c80c3ccbb65fed1ecc8789cf7
parent9f9c8d63a5822378e18f3f8f188df21c6104e18e
c: Give permerror for excess braces in scalar initializers [PR88642]

As noted in bug 88642, the C front end fails to give errors or
pedwarns for scalar initializers with too many levels of surrounding
braces.  There is a warning for redundant braces around a scalar
initializer within a larger braced initializer (valid for a single
such level within a structure, union or array initializer; not valid
for more than one such level, or where the outer layer of braces is
itself for a scalar, either redundant braces themselves or part of a
compound literal), but this never becomes an error even for invalid
cases.  Check for this case and turn the warning into a permerror when
there are more levels of braces than permitted.  The existing warning
is unchanged for a single (permitted) level of redundant braces around
a scalar initializer inside a structure, union or array initializer,
and it's also unchanged that no such warning is given for a single
(permitted) level of redundant braces around a top-level scalar
initializer.

Technically this is a C2y issue (these rules on valid initializers
moved into Constraints as a result of N3346, accepted in Minneapolis;
previously, as a "shall" outside constraints, violating these rules
resulted in compile-time undefined behavior without requiring a
diagnostic).

Hopefully little code is actually relying on not getting an error
here.  In view of gcc.dg/tree-ssa/ssa-dse-10.c showing that at least
some code may be using such over-braced initializers (initializer of
pubKeys at line 1167 in that test; I'm not at all sure how that
initializer ends up getting interpreted to translate it to something
equivalent but properly structured), this is made a permerror rather
than a hard error, so -fpermissive (as already used by that test) can
be used to disable the error (the default -fpermissive for old
standards modes is not a problem given that before C2y this is
undefined behavior not a constraint violation).

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

PR c/88642

gcc/c/
* c-typeck.cc (constructor_braced_scalar): New variable.
(struct constructor_stack): Add braced_scalar field.
(really_start_incremental_init): Handle constructor_braced_scalar
and braced_scalar field.
(push_init_level): Handle constructor_braced_scalar and
braced_scalar field.  Give permerror rather than warning for
nested braces around scalar initializer.
(pop_init_level): Handle constructor_braced_scalar and
braced_scalar field.

gcc/testsuite/
* gcc.dg/c2y-init-1.c: New test.
gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/c2y-init-1.c [new file with mode: 0644]