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

            Bug ID: 118323
           Summary: Unhelpful diagnostic for consteval failure inside
                    lambda that is immediate-escalating
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

This C++20 program gives a nice clear diagnostic:

consteval int check(int i) { if (i > 100) throw; return i; }

void funk(int) { }

void func()
{
#ifdef LAMBDA
  []{
    funk(check(101));
  }();
#else
  funk(check(101));
#endif
}

int main()
{
  func();
}


But if compiled with -DLAMBDA the diagnostic no longer says what's wrong:

esc.cc: In function 'void func()':
esc.cc:10:4: error: call to consteval function '<lambda closure
object>func()::<lambda()>().func()::<lambda()>()' is not a constant expression
    8 |   []{
      |   ~~~
    9 |     funk(check(101));
      |     ~~~~~~~~~~~~~~~~~
   10 |   }();
      |   ~^~
esc.cc:10:4: error: 'func()::<lambda()>' called in a constant expression
esc.cc:8:3: note: 'func()::<lambda()>' is not usable as a 'constexpr' function
because:
    8 |   []{
      |   ^
esc.cc:9:9: error: call to non-'constexpr' function 'void funk(int)'
    9 |     funk(check(101));
      |     ~~~~^~~~~~~~~~~~
esc.cc:3:6: note: 'void funk(int)' declared here
    3 | void funk(int) { }
      |      ^~~~
esc.cc:9:15: note: 'func()::<lambda()>' was promoted to an immediate function
because its body contains an immediate-escalating expression 'check(101)'
    9 |     funk(check(101));
      |          ~~~~~^~~~~


This is completely unhelpful. It says nothing about the error inside check(101)
which makes it not a constant expression. Obviously the lambda body can't be a
constant expression, because it calls func(int) which is not constexpr.

Reply via email to