llvm / llvm/llvm-project

[Clang] `if consteval {}` causes more constant evaluation steps than an empty statement

Open
#184,985 3 comments 0 reactions 0 assignees View on GitHub
clang:frontend constexpr documentation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This example shows that Clang currently considers `if consteval {}` causes more constant evaluation steps than `;`. [Godbolt link](https://godbolt.org/z/acGs7KsEY).
```C++
constexpr bool test_empty_if_consteval(int n) {
for (int i = 0; i < n; ++i)
if consteval {}
return true;
}

constexpr bool test_empty(int n) {
for (int i = 0; i < n; ++i)
;
return true;
}

static_assert(test_empty_if_consteval(524286));
static_assert(test_empty(1048572));
static_assert(test_empty_if_consteval(524287)); // fails by default
static_assert(test_empty(1048573)); // fails by default
```

If I understood correctly, the standard treats "full-expressions evaluated within a core constant expression" as the implementation-defined limit ([[expr.const]/9.7](https://eel.is/c++draft/expr.const#9.7), [[implimits]/1.39](https://eel.is/c++draft/implimits#1.39)), and neither `if consteval {}` nor `;` contains any full-expression ([[stmt.if]/4](https://eel.is/c++draft/stmt.if#4), [[intro.execution]/5](https://eel.is/c++draft/intro.execution#5)). So it seems to me that there shouldn't be any difference on constant evaluation limits between `if consteval {}` and `;`.

Is such difference intended? If so, perhaps we should amend the standard wording to permit our strategy.

Edit: Perhaps we should enhance [our documentation](https://clang.llvm.org/docs/UsersManual.html#cmdoption-fconstexpr-steps) in some way.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.