Skip to content

Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed #576

Description

@daveedvdv-nvidia

Issue Summary

The C++-generating back end can name a closure type only through a typedef decltype(v) __Tn that it puts out after the declaration of a variable v of that type. Two problems follow:

  1. A cast to a closure type for which no such typedef was put out is generated with the closure type written as if it were a class name, for example class __T1{...}, which is ill-formed. Such a cast can only be written with auto (C++23): auto{x} or auto(x), where x is a lambda-expression, a call returning a closure object, or a closure variable for which no typedef is put out (for example, one declared in a for init).
  2. For a variable of closure type declared in the condition of an if, switch, or while statement, or in a range-based for statement, the typedef is put out inside the declaration, which is also ill-formed. The second problem doesn't need C++23.

The front end itself accepts the source. This came up while looking at PR #575 (for #572).

Reproducing Source Code

int i = 3;
template<class T> int size(T&&) { return sizeof(T); }
int x = auto{[](int a) { return a; }}(2);                       // 1
int y = size(auto{[]{}});                                       // 2
int f() { int j = 1; return auto{[&] { return i + j; }}(); }    // 3
auto g() { return [](int a) { return a; }; }
int z = auto{g()}(2);                                           // 4
int h() {
  for (auto m = [n = 0]() mutable { return ++n; }; ; )
    return auto{m}() + auto(m)();                               // 5
}
int k() {
  if (auto m = [] { return 1; }) return m();                    // 6
  return 0;
}

Command-line Options

cpfe-cp --c++23 --gen_c_file_name t.int.c t.cpp

Type of Issue

[Back End] There is a problem with the generated C++ code.

Additional Details

GCC and Clang accept the source, as does cpfe --c++23 (without C++ generation).

The generated C++ for the marked lines is:

int x = class __T1{[](int a) { return a; } }(2);
int y = size(class __T2{[]{ } });
int f() { int j = 1; return class __T3{[&]{ return i + j; } }(); }
int z = class __T4{g()}(2);
for (auto m = [n = 0]() mutable { return ++n; } ; ;) { return class __T5{m}() + ((__T5)(m))(); }
if (auto m = []{ return 1; } typedef decltype(m) __T6;) { return m(); }

which GCC rejects (expected primary-expression before 'class', 'typedef' invalid in condition, ...). A range-based for gives, e.g., for (auto m typedef decltype(m) __T1; : r).

Related forms are generated correctly:

  • A cast of a closure variable with a typedef, like auto m = [](int a) { return a; }; auto{m}(1), becomes __T1{m}(1).
  • The parenthesized form of a prvalue operand, like auto([](int a) { return a; })(2), becomes ([](int a) { return a; } )(2).
  • An initializer like auto g = auto{[]{ return 1; }}; becomes auto g = []{ return 1; } ;.

The problem already occurs on main. It shows up in the edg_x86_64_cp and edg_x86_32_cp test configurations, which compile the generated C++.

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Activity

  1. changed the title [-]Generated C++ for a braced cast of a lambda-expression names the closure type[/-] [+]Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed[/+] on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions