Skip to content

call to consteval in immediate invocation context fails #51182

Closed
@llvmbot

Description

@llvmbot
Bugzilla Link 51840
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie,@zygoloid

Extended Description

Reduced from https://stackoverflow.com/questions/69166564

The following code fails on clang >= 11.0 (including trunk)

consteval int f(int) { return 1; }

template
consteval bool g(T a)
{
int n = f(a); // error here
return true;
}

static_assert(g(2));


with
error: call to consteval function 'f' is not a constant expression
note: read of non-const variable 'a' is not allowed in a constant expression

This seems incorrect. The evaluation of f is in an immediate context, i.e. inside g so f(a) should be a constant expression. Also, the error only appears if g is a template, and if the result of f(a) is stored in a local variable. GCC accepts the code.

Metadata

Metadata

Assignees

Labels

bugzillaIssues migrated from bugzillac++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partyconstevalC++20 consteval

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions