llvm / llvm/llvm-project

[Clang] consteval use of earlier member in DMI rejected during aggregate initialization

Open
#207,064 4 comments 0 reactions 0 assignees View on GitHub
clang:frontend confirmed consteval diverges-from:gcc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Clang rejects a default member initializer that uses an earlier member of the object being aggregate-initialized in a consteval call, even when that earlier member is initialized with a constant expression.

Reproducer (credit to @efriedma-quic):

```cpp
struct Noisy {
int x;
consteval Noisy(int x) : x(x) {}
constexpr ~Noisy() {}
};

struct Function {
template constexpr Function(F) {}
};

struct Options {
int x;
Function function{Noisy{x}};
};

Options kOptions{5};
```
GCC accepts this under C++20 mode. Clang currently rejects this with a diagnostic along the lines of:

```text
error: call to consteval function 'Noisy::Noisy' is not a constant expression
note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
```
This seems incorrect. In the default member initializer, `x` refers to the member of the object being constructed, and during aggregate initialization `Options kOptions{5};` initializes `x` with a constant expression before evaluating the default member initializer for `function`. GCC accepts this testcase.

Godbolt: https://godbolt.org/z/nno6MsxdW

Contributor guide

Open the contributing guide

Research direction

Start with the provided C++20 reproducer and compare Clang's behavior with GCC using the linked Godbolt example. Trace constant evaluation of the default member initializer during aggregate initialization, then add a regression test showing that the consteval call is accepted when the earlier member has a constant initializer.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.