https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123531
Bug ID: 123531
Summary: [16 Regression] ICE: Segmentation fault in
build_new_op when accessing uncaptured variable in
lambda within a malformed constructor/member function.
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jirehguo at tju dot edu.cn
Target Milestone: ---
Compiler Explorer: https://godbolt.org/z/1aEnf349G
The following c++ code crash gcc trunk. But 13, 14, 15 is fine.
Code:
```cpp
#include <atomic>
template <> struct A {
public:
C() : B() {
std::atomic<bool> &done = 0;
auto spin_loop = [] {
while (!done) {
}
}
}
};
```
Stack dump:
```
<source>:2:22: error: explicit specialization of non-template 'A'
2 | template <> struct A {
| ^
<source>:4:3: error: ISO C++ forbids declaration of 'C' with no type
[-fpermissive]
4 | C() : B() {
| ^
<source>: In member function 'int A::C()':
<source>:4:9: error: only constructors take member initializers
4 | C() : B() {
| ^
<source>:4:9: error: class 'A' does not have any field named 'B'
<source>:5:31: error: cannot bind non-const lvalue reference of type
'std::atomic<bool>&' to an rvalue of type 'std::atomic<bool>'
5 | std::atomic<bool> &done = 0;
| ^
In file included from <source>:1:
/cefs/78/78a5e4de62452ef24e2dd067_gcc-trunk-20260111/include/c++/16.0.0/atomic:85:15:
note: after user-defined conversion: 'constexpr
std::atomic<bool>::atomic(bool)'
85 | constexpr atomic(bool __i) noexcept : _M_base(__i) { }
| ^~~~~~
<source>: In lambda function:
<source>:7:15: error: 'done' is not captured
7 | while (!done) {
| ^~~~
<source>:6:23: note: the lambda has no capture-default
6 | auto spin_loop = [] {
| ^
<source>:5:24: note: 'std::atomic<bool>& done' declared here
5 | std::atomic<bool> &done = 0;
| ^~~~
<source>:7:15: internal compiler error: Segmentation fault
7 | while (!done) {
| ^~~~
0x2924d48 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
???:0
0x2919b0b internal_error(char const*, ...)
???:0
0xb52266 build_new_op(op_location_t const&, tree_code, int, tree_node*,
tree_node*, tree_node*, tree_node*, tree_node**, int)
???:0
0xe2c6e4 build_x_unary_op(unsigned long, tree_code, cp_expr, tree_node*, int)
???:0
0xddbcc4 finish_unary_op_expr(unsigned long, tree_code, cp_expr, int)
???:0
0xd573b3 c_parse_file()
???:0
0xec8de9 c_common_parse_file()
???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1
```