https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123346
Bug ID: 123346
Summary: Segmentation fault when returning union initialized
using temporary object at call site, with designated
initializers
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: janschultke at googlemail dot com
Target Milestone: ---
https://godbolt.org/z/cx91MPT4v
The following valid code results in a segfault for GCC 16:
struct Unit { };
union Union { Unit unit; };
constexpr Union make(Union&& other) {
return { .unit = other.unit };
}
constexpr Union u = make(Union { .unit = Unit{} });
------------------------------------------------------------------
Segmentation fault
6 | constexpr Union u = make(Union { .unit = Unit{} });
| ^
0x290fae8 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
???:0
0x29048ab internal_error(char const*, ...)
???:0
0xc3d83b expr_to_string(tree_node*)
???:0
0x29713d3 pretty_printer::format(text_info&)
???:0
0x290f3e3
diagnostics::context::report_diagnostic(diagnostics::diagnostic_info*)
???:0
0x290fae8 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
???:0
0x2903c8d error_at(unsigned long, char const*, ...)
???:0
0xe43a9e store_init_value(tree_node*, tree_node*, vec<tree_node*, va_gc,
vl_embed>**, int)
???:0
0xc1a02e cp_finish_decl(tree_node*, tree_node*, bool, tree_node*, int,
cp_decomp*)
???:0
0xd52ed3 c_parse_file()
???:0
0xec4909 c_common_parse_file()
???:0
------------------------------------------------------------------
For GCC 15, this prints nonsense:
<source>:6: confused by earlier errors, bailing out
Compiler returned: 1
------------------------------------------------------------------
For GCC 14 and below, this rejects valid code with a nonsensical diagnostic:
<source>:6:50: error: 'Union{}' is not a constant expression
6 | constexpr Union u = make(Union { .unit = Unit{} });
| ^
Compiler returned: 1