https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125403

            Bug ID: 125403
           Summary: ICE with contract postcondition on in-class defaulted
                    constructor
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

The following valid C++26 code triggers an internal compiler error when
compiling with GCC contracts enabled.

Source:

```cpp
struct Foo {
    Foo()
        post(empty())
        = default;

    bool empty() const { return ptr == nullptr; }

    int* ptr = nullptr;
};

void f() {
    Foo f;
}
```

Compiler invocation: `g++ -std=c++26 repro.cpp`

Observed behavior:

GCC ICEs during compilation.

Expected behavior:

The program should compile successfully.

Compiler Explorer repro: https://godbolt.org/z/64j3acYr7

Observed with:

* GCC 15.2 + `-fcontracts`
* GCC 16.1
* GCC trunk

Does not reproduce with: Clang contracts branch

Workarounds:

1. Removing the contract:

```cpp
Foo() = default;
```

2. Using a user-provided constructor body:

```cpp
Foo() post(empty()) {}
```

3. Defaulting the constructor out-of-line:

```cpp
struct Foo {
  Foo() post(empty());
  ~~~
};

Foo::Foo() = default;
```

Notes:

The issue appears specifically related to an in-class defaulted constructor
with a contract postcondition.

The code appears well-defined because the postcondition is evaluated after
object construction completes and after `ptr` has been initialized.

Please see CE link for ICE output and stack trace.

Reply via email to