From adfa5d960c7da1f909c068612f32e818b7e58f8c Mon Sep 17 00:00:00 2001 From: Daveed Vandevoorde Date: Fri, 20 Nov 2026 15:56:38 +0000 Subject: [PATCH] Generate casts to unnamed closure types and closure-type condition variables correctly [GH #576] --- src/cp_gen_be.c | 60 ++++++++++++- .../gh-576-closure-braced-cast.txt | 41 +++++++++ .../tests/changes/.GH576.rto/default.1.1.txt | 1 + tests/tests/changes/GH576.sft.cpp | 87 +++++++++++++++++++ 4 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/pending_changes/gh-576-closure-braced-cast.txt create mode 100644 tests/tests/changes/.GH576.rto/default.1.1.txt create mode 100644 tests/tests/changes/GH576.sft.cpp diff --git a/src/cp_gen_be.c b/src/cp_gen_be.c index 52e8dd0a3b..db00d27cc1 100644 --- a/src/cp_gen_be.c +++ b/src/cp_gen_be.c @@ -23128,6 +23128,45 @@ to add extra parentheses to disambiguate. } /* expr_may_look_like_type */ +static a_boolean is_single_operand_cast_to_unnamed_closure( + a_dynamic_init_ptr dip, + a_type_ptr type) +/* +dip is the dynamic initialization for an explicit cast to type. Return TRUE +if type is a closure type that has no name in the generated code (because no +typedef was put out for it; see gen_variable_decl) and the cast has a single +operand. +*/ +{ + a_boolean result = FALSE; + + type = skip_non_naming_typerefs(type); + if (is_immediate_class_type(type) && + class_type_supp(type)->is_lambda_closure_class && + !type->has_been_declared) { + an_expr_node_ptr args; + switch (dip->kind) { + case dik_expression: + case dik_lambda: + result = TRUE; + break; + case dik_bitwise_copy: + result = (dip->variant.bitwise_copy.source != NULL); + break; + case dik_constructor: + args = dip->variant.constructor.args; + result = (args != NULL && !args->generated_default_arg && + !args->is_pack_expansion && + (args->next == NULL || args->next->generated_default_arg)); + break; + default: + break; + } /* switch */ + } /* if */ + return result; +} /* is_single_operand_cast_to_unnamed_closure */ + + static void handle_braced_init_list_ctor(an_expr_node_ptr arg) /* arg is the first constructor argument in a braced dik_constructor dynamic @@ -23212,6 +23251,7 @@ when possible. an_expr_node_ptr expr = NULL; int closing_parens_needed = 0; a_boolean unnamed_type_case = FALSE; + a_boolean unnamed_closure_case = FALSE; a_boolean braced_init; a_boolean is_value_init; a_type_ptr bare_init_entity_type; @@ -23353,6 +23393,13 @@ when possible. use_func_notation_cast = FALSE; unnamed_type_case = TRUE; } /* if */ + if (is_single_operand_cast_to_unnamed_closure(dip, init_entity_type)) { + /* The closure type has no name here, so the cast must have been + written "auto{x}" or "auto(x)", and it is put out that way. */ + use_func_notation_cast = TRUE; + unnamed_type_case = FALSE; + unnamed_closure_case = TRUE; + } /* if */ /* We've now decided whether or not we want to use a functional-notation cast. Change that decision in some cases to deal with deficiencies in some target compilers. */ @@ -23407,9 +23454,13 @@ when possible. /* Use a functional-notation cast. */ output_functional_notation_cast: octl.suppress_template_args = suppress_template_args; - gen_type_reference(init_entity_type, /*suppress_typename_kwd=*/FALSE, - /*is_declaration=*/FALSE, - /*suppress_elab_type_spec=*/TRUE); + if (unnamed_closure_case) { + write_tok_str("auto"); + } else { + gen_type_reference(init_entity_type, /*suppress_typename_kwd=*/FALSE, + /*is_declaration=*/FALSE, + /*suppress_elab_type_spec=*/TRUE); + } /* if */ octl.suppress_template_args = saved_suppress_template_args; /* In a case like "auto x = T{};", braced-init will be FALSE, reflecting the use of the "=" in the initializer. However, if T @@ -24772,7 +24823,8 @@ this one is such a continuation. adjust_current_namespace(orig_scope, common_scope); } /* if */ var->declaration_has_been_put_out = TRUE; - if (var->declared_with_auto_type_specifier && !for_init && + if (var->declared_with_auto_type_specifier && + !for_init && !is_condition && !is_iterator && is_immediate_class_type(unqual_var_type) && unqual_var_type->variant.class_struct_union.extra_info-> is_lambda_closure_class && diff --git a/src/pending_changes/gh-576-closure-braced-cast.txt b/src/pending_changes/gh-576-closure-braced-cast.txt new file mode 100644 index 0000000000..93baf0183c --- /dev/null +++ b/src/pending_changes/gh-576-closure-braced-cast.txt @@ -0,0 +1,41 @@ +[GH #576] +C++-generating back end: casts to unnamed closure types + +The C++-generating back end can name a closure type only through a typedef +that it puts out after the declaration of a variable of that type. A cast to +a closure type for which there was no such typedef (which can be written +only with "auto", in C++23) was put out with the closure type written as if +it were a class name, which made the generated code ill-formed. For +example, with --c++23: + + int x = auto{[](int a) { return a; }}(2); + auto g() { return [](int a) { return a; }; } + int y = auto{g()}(2); + int f() { + for (auto m = [n = 0]() mutable { return ++n; }; ; ) return auto{m}(); + } + +was generated as + + int x = class __T1{[](int a) { return a; } }(2); + auto g() { return [](int a) { return a; } ; } + int y = class __T2{g()}(2); + int f() { + for (auto m = [n = 0]() mutable { return ++n; } ; ;) { + return class __T3{m}(); } + } + +Such a cast is now put out as written, with "auto". + +Also, 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 +was put out inside the declaration. For example, + + if (auto m = [] { return 1; }) return m(); + +was generated as + + if (auto m = []{ return 1; } typedef decltype(m) __T4;) { return m(); } + +No typedef is put out for such a variable now (as was already the case for a +variable declared in a for-init). diff --git a/tests/tests/changes/.GH576.rto/default.1.1.txt b/tests/tests/changes/.GH576.rto/default.1.1.txt new file mode 100644 index 0000000000..4163d8bb9a --- /dev/null +++ b/tests/tests/changes/.GH576.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++23 Test_name.c diff --git a/tests/tests/changes/GH576.sft.cpp b/tests/tests/changes/GH576.sft.cpp new file mode 100644 index 0000000000..d69ffe2c3d --- /dev/null +++ b/tests/tests/changes/GH576.sft.cpp @@ -0,0 +1,87 @@ +//type:fp +//options:--c++23 +//remark:Casts to unnamed closure types and closure typedefs in generated C++ +// +// In generated C++, a cast to a closure type for which no typedef had been +// put out, like auto{[](int a) { return a; }} or auto{m} with m declared in a +// for-init, was put out with the unnamed closure type written as if it were +// a class name. Also, a typedef for the closure type of a variable declared +// in a condition or range-based for statement was put out inside the +// declaration. + +int i = 3; +template constexpr int size(T&&) { return sizeof(T); } +struct C { C(); C(const C&); int v; }; +auto g() { return [](int a) { return a; }; } + +static_assert(auto{[](int a) { return a; }}(2) == 2); +static_assert(auto{auto{[] { return 1; }}}() == 1); +static_assert(size(auto{[] {}}) > 0); +int x = auto{[](int a) { return a + i; }}(2); +int y = sizeof(auto{[] {}}) + size(auto{[] {}}); +auto h1 = auto{[] { return 1; }}; +int z = auto{g()}(2) + auto(g())(3); + +int f1() { + int j = 1; + return auto{[&] { return i + j; }}() + + auto{[j]() mutable { return ++j; }}() + + auto{[](auto a) { return a; }}(j) + + auto{[c = C()] { return c.v; }}(); +} + +template T f2() { return auto{[] { return T(1); }}(); } +int w = f2(); + +int f3() { + int n = 0; + for (auto m = [n = 0]() mutable { return ++n; }; auto{m}() < 3; auto(m)()) { + n += m(); + } + for (const auto c = [k = 0]() mutable { return ++k; }; n < 9; ++n) { + n += auto{c}(); + } + for (auto m = [c = C()] { return c.v; }; n < 12; ++n) { + n += auto{m}() + auto(m)(); + } + for (auto m = g(); n < 15; ++n) { + n += auto{m}(1); + } + return n; +} + +int f4() { + return [m = [n = 0]() mutable { return ++n; }]() mutable { + return auto{m}() + m(); + }(); +} + +struct R { + struct It { + int n; + auto operator*() { return g(); } + It& operator++() { ++n; return *this; } + bool operator!=(It o) const { return n != o.n; } + }; + It begin() { return {0}; } + It end() { return {2}; } +}; + +int f5() { + int n = 0; + if (auto m = [] { return 1; }) { + n += m() + auto{m}(); + } + switch (auto m = [] { return 2; }; m()) { + default: + n += m(); + } + while (auto m = [] { return 3; }) { + n += m(); + break; + } + for (auto m : R{}) { + n += m(1) + auto{m}(2); + } + return n; +}