Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] [Sema] C++23 consteval if not recognised as immediate function context in templated function #91509

Closed
MitalAshok opened this issue May 8, 2024 · 2 comments · Fixed by #91939
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party consteval C++20 consteval

Comments

@MitalAshok
Copy link
Contributor

Consider https://godbolt.org/z/qTexTzKrb:

consteval int f(int) { return 0; }

template<typename T>
constexpr int g(int x) {
    if consteval {
        return f(x);
    }
    return 1;
}

int h(int x) {
    return g<void>(x);
}

The invocation being inside the if consteval means it's in an immediate function context ([expr.const]p(16.3)), so it shouldn't be immediate-escalating, and g<void> should not be an immediate function, yet Clang complains that it is.

<source>:12:12: error: call to immediate function 'g<void>' is not a constant expression
   12 |     return g<void>(x);
      |            ^
<source>:6:16: note: 'g<void>' is an immediate function because its body contains a call to a consteval function 'f' and that call is not a constant expression
    6 |         return f(x);
      |                ^~~~
<source>:12:20: note: function parameter 'x' with unknown value cannot be used in a constant expression
   12 |     return g<void>(x);
      |                    ^
<source>:11:11: note: declared here
   11 | int h(int x) {
      |           ^
@github-actions github-actions bot added the clang Clang issues not falling into any other category label May 8, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval and removed clang Clang issues not falling into any other category labels May 8, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 8, 2024

@llvm/issue-subscribers-clang-frontend

Author: Mital Ashok (MitalAshok)

Consider <https://godbolt.org/z/qTexTzKrb>:
consteval int f(int) { return 0; }

template&lt;typename T&gt;
constexpr int g(int x) {
    if consteval {
        return f(x);
    }
    return 1;
}

int h(int x) {
    return g&lt;void&gt;(x);
}

The invocation being inside the if consteval means it's in an immediate function context ([[expr.const]p(16.3)](https://eel.is/c++draft/expr.const#16.3)), so it shouldn't be immediate-escalating, and g&lt;void&gt; should not be an immediate function, yet Clang complains that it is.

&lt;source&gt;:12:12: error: call to immediate function 'g&lt;void&gt;' is not a constant expression
   12 |     return g&lt;void&gt;(x);
      |            ^
&lt;source&gt;:6:16: note: 'g&lt;void&gt;' is an immediate function because its body contains a call to a consteval function 'f' and that call is not a constant expression
    6 |         return f(x);
      |                ^~~~
&lt;source&gt;:12:20: note: function parameter 'x' with unknown value cannot be used in a constant expression
   12 |     return g&lt;void&gt;(x);
      |                    ^
&lt;source&gt;:11:11: note: declared here
   11 | int h(int x) {
      |           ^

@tbaederr
Copy link
Contributor

tbaederr commented May 9, 2024

CC @cor3ntin

@cor3ntin cor3ntin added the confirmed Verified by a second party label May 9, 2024
cor3ntin added a commit that referenced this issue May 13, 2024
…text (#91939)

We did not set the correct evaluation context for the compound statement
of an ``if consteval`` statement
in a templated entity in TreeTransform.

Fixes #91509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party consteval C++20 consteval
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants