https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125771
procoinmac0 at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |procoinmac0 at gmail dot com
--- Comment #6 from procoinmac0 at gmail dot com ---
I ran into the same bug independently and reduced it a little further. Some
details that may help to confirm it.
1. It does not need designated initializers, C++20 or the library. This
header-free testcase aborts with g++ 12.5.0 through trunk, also with
-std=c++98:
int ctors, dtors;
struct A {
A () { ++ctors; }
A (const A &) { ++ctors; }
~A () { ++dtors; }
};
struct Inner { A a; int k; };
struct Outer { Inner in; A t; };
A make () { return A (); }
A boom () { throw 1; }
int
main ()
{
try
{
Outer o = { { make (), 0 }, boom () };
}
catch (int)
{
}
// One A was constructed (o.in.a), so one must be destroyed.
if (ctors != 1 || dtors != 1)
__builtin_abort ();
}
$ g++ -O0 pr.cpp && ./a.out
Aborted (core dumped)
ctors is 1 and dtors is 2. The result is the same at -O2. There are no
diagnostics with -Wall -Wextra, and -fsanitize=address,undefined reports
nothing (the abort is the testcase's own check).
2. What is needed, from variants of the testcase:
(a) the nested aggregate is initialized from a braced list without a type
name: plain nested braces as above, brace elision, or designated
initializers as in comment 0. With the type name written,
"Inner{ make (), 0 }", the program is correct;
(b) at least one member of the nested aggregate gets a constant
initializer: an explicit literal as above, an omitted member that is
value-initialized (the "int i" of comment 0), or a constant default
member initializer. If the "0" above is replaced by a non-constant
value (e.g. an int read from a global), the program is correct;
(c) a later element of the enclosing list throws.
Each further enclosing nested level adds one more destruction of the same
member.
3. -fdump-tree-gimple of main (16.2.0, -O0) shows the two cleanups that
comment 2 describes, both still armed when boom () throws:
o = {};
o.in.a = make (); [return slot optimization]
D.3055 = 1;
try
{
D.3065 = 1;
try
{
o.t = boom (); [return slot optimization]
D.3055 = 0;
D.3065 = 0;
}
finally
{
if (D.3065 != 0) goto <D.3085>; else goto <D.3086>;
<D.3085>:
Inner::~Inner (&o.in);
...
}
}
finally
{
if (D.3055 != 0) goto <D.3082>; else goto <D.3083>;
<D.3082>:
A::~A (&o.in.a);
...
}
With a non-constant value for k, only the cleanup for o.in.a is emitted
(and there is no "o = {}" store), and the program is correct.
4. Versions tested (x86_64-linux-gnu):
releases 5.5.0, 7.5.0, 9.5.0, 10.5.0, 11.5.0 exit 0
releases 12.5.0, 15.3.0, 16.2.0 abort
trunk 17.0.0 20261112 (Compiler Explorer build) abort
Ubuntu's 13.3.0 (x86_64 and aarch64) and 14.2.0 abort
clang++ (15.0.7, 19.1.1) and MSVC (19.44, 19.51) destroy o.in.a once.
(I posted this to gcc-help on 2026-11-12 before I found this PR.)