https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104403
Bug ID: 104403
Summary: ICE while optimizing lambda that returns address of a
static variable hidden in a switch
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: franwade33 at googlemail dot com
Target Milestone: ---
On gcc 12.0.1, the following gives an internal compiler error:
// g++-12 -std=c++17 -O1
// g++-12 -std=c++20 -O1
int main() {
[]{
switch (0) {
case 0:
static int value;
return &value;
}
};
}
Reproduced here: https://godbolt.org/z/5PKGjGo33
This is the compiler output:
<source>: In static member function 'static constexpr int*
main()::<lambda()>::_FUN()':
<source>:8:5: in 'constexpr' expansion of '0->main()::<lambda()>()'
<source>:8:5: internal compiler error: in cxx_eval_constant_expression,
at cp/constexpr.cc:6652
8 | };
| ^
0x2169b09 internal_error(char const*, ...)
???:0
0x73cd21 fancy_abort(char const*, int, char const*)
???:0
0x7bc242 maybe_constant_value(tree_node*, tree_node*, bool)
???:0
0x87d5db fold_for_warn(tree_node*)
???:0
0xa9048e check_return_expr(tree_node*, bool*)
???:0
0xa19a9e finish_return_stmt(tree_node*)
???:0
0x8997f3 maybe_add_lambda_conv_op(tree_node*)
???:0
0x97f7ed c_parse_file()
???:0
0xb0f1c2 c_common_parse_file()
???:0
It compiles fine on gcc 11.2.0, and it also compiles fine with -std=c++2b (and
-std=c++14, where a closure type's operator() can't be implicitly constexpr).
Probably has something to do with C++2b P2242R3 allowing static variables in
constexpr functions.