diff --git a/src/cp_gen_be.c b/src/cp_gen_be.c index 52e8dd0a3b..e727852af7 100644 --- a/src/cp_gen_be.c +++ b/src/cp_gen_be.c @@ -23353,6 +23353,12 @@ when possible. use_func_notation_cast = FALSE; unnamed_type_case = TRUE; } /* if */ + if (dip->is_auto_cast) { + /* The cast was written "auto(x)" or "auto{x}", so put it out that way + (the type might not be nameable, e.g., if it is a closure type). */ + use_func_notation_cast = TRUE; + unnamed_type_case = FALSE; + } /* 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 +23413,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 (dip->is_auto_cast) { + 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 +24782,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/expr.c b/src/expr.c index 358c42d016..cefe69d085 100644 --- a/src/expr.c +++ b/src/expr.c @@ -30741,6 +30741,8 @@ previously-scanned braced initializer. an_init_component_ptr icp; a_conv_context_set conv_context = CCO_CAST | CCO_EXPLICIT_CAST; a_boolean error_on_narrowing; + a_boolean is_auto_cast = FALSE; + an_expr_node_ptr auto_cast_operand = NULL; an_expr_node_ptr expr; check_assertion(list_init_enabled); @@ -30765,6 +30767,7 @@ previously-scanned braced initializer. cast_operand_to_void(result, type_cast_to); } else { if (is_auto_type(type_cast_to)) { + is_auto_cast = TRUE; if (!auto_cast_enabled) { expr_pos_warning(ec_auto_cast_is_cpp23, &error_position); } /* if */ @@ -30776,6 +30779,7 @@ previously-scanned braced initializer. if (!is_template_dependent_type(opnd->type)) { type_cast_to = type_after_auto_cast_transformations(opnd->type); } /* if */ + auto_cast_operand = expr_node_from_operand(opnd); } else { expr_pos_error(ec_bad_cast, init_component_pos(icp)); type_cast_to = error_type(); @@ -30798,10 +30802,23 @@ previously-scanned braced initializer. /* Record source form information in the associated IL entries if needed. */ expr->is_brace_notation_cast = TRUE; - if (expr->kind == (an_expr_node_kind)enk_temp_init) { + if (is_auto_cast && auto_cast_operand != NULL && + (expr == auto_cast_operand || + (node_is(expr, enk_temp_init) && + node_is(auto_cast_operand, enk_temp_init) && + expr->variant.init.dynamic_init == + auto_cast_operand->variant.init.dynamic_init))) { + /* The operand is a prvalue of the type cast to, as in "auto{T(x)}", + so the result is just the operand, which keeps its own source + form. */ + } else if (node_is(expr, enk_temp_init)) { a_dynamic_init_ptr dip = expr->variant.init.dynamic_init; dip->is_explicit_cast = TRUE; dip->is_braced_initializer = TRUE; + if (is_auto_cast) { + dip->is_auto_cast = TRUE; + skip_constexpr_init_folding(dip)->is_auto_cast = TRUE; + } /* if */ if (dip->kind == (a_dynamic_init_kind)dik_constant || dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate) { dip->variant.constant.ptr->explicit_cast_applied = TRUE; @@ -30964,7 +30981,8 @@ freed by this routine. a_boolean err = FALSE; a_symbol_ptr ctor_sym = NULL; a_boolean ctor_case = FALSE, force_dependent = FALSE; - a_boolean could_be_dependent = FALSE, is_auto_cast; + a_boolean could_be_dependent = FALSE; + a_boolean is_auto_cast = FALSE; a_class_symbol_supplement_ptr cssp = NULL; an_operand local_bound_function_selector; a_boolean allow_ms_array = microsoft_bugs && !C_mode(); @@ -31657,6 +31675,11 @@ freed by this routine. expr->is_functional_notation_cast = TRUE; if (expr->kind == (an_expr_node_kind)enk_temp_init) { dip = expr->variant.init.dynamic_init; + if (is_auto_cast && dip->is_explicit_cast && + expr != orig_operand_expression) { + dip->is_auto_cast = TRUE; + skip_constexpr_init_folding(dip)->is_auto_cast = TRUE; + } /* if */ } /* if */ } /* if */ set_operand_position(result, start_position, &end_position, start_position); diff --git a/src/il_alloc.c b/src/il_alloc.c index aa73a8d28e..3e972a3ecd 100644 --- a/src/il_alloc.c +++ b/src/il_alloc.c @@ -2434,6 +2434,7 @@ Initialize a dynamic_init entry of the kind specified. dip->is_explicit_cast = FALSE; dip->is_compound_literal = FALSE; dip->is_braced_initializer = FALSE; + dip->is_auto_cast = FALSE; dip->is_partially_initialized = FALSE; dip->is_result_for_class_rvalue_question_mark = FALSE; dip->class_rvalue_initialized_through_master_entry = FALSE; diff --git a/src/il_def.h b/src/il_def.h index 886cd131b2..ae84dbb79b 100644 --- a/src/il_def.h +++ b/src/il_def.h @@ -4155,6 +4155,10 @@ typedef struct a_dynamic_init { a_bit_field is_braced_initializer:1; /* If TRUE, the source construct that generated this initialization is a brace-enclosed initializer. */ + a_bit_field is_auto_cast:1; + /* If TRUE, is_explicit_cast is also TRUE, and the + cast was written with "auto" as its type (C++23), + as in "auto(x)" or "auto{x}". */ a_bit_field is_partially_initialized:1; /* TRUE if the initialized entity is an array or class aggregate not completely initialized by the diff --git a/src/il_display.c b/src/il_display.c index 82a8e318a2..cd9b9d36f7 100644 --- a/src/il_display.c +++ b/src/il_display.c @@ -7370,6 +7370,9 @@ Display the indicated dynamic_init structure. if (ptr->is_braced_initializer) { disp_boolean("is_braced_initializer", TRUE); } /* if */ + if (ptr->is_auto_cast) { + disp_boolean("is_auto_cast", TRUE); + } /* if */ if (ptr->is_partially_initialized) { disp_boolean("is_partially_initialized", TRUE); } /* if */ 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..4a0a5a4f53 --- /dev/null +++ b/src/pending_changes/gh-576-closure-braced-cast.txt @@ -0,0 +1,25 @@ +[GH #576] +C++-generating back end: auto casts + +A cast written with "auto" (C++23), as in "auto(x)" or "auto{x}", was put +out as a cast to the deduced type. When that was a closure type, the +generated code was often ill-formed: if no variable of that type had been +declared, the type was written as "class __Tn", and if the variable was +const, the cast yielded a const object. For example, with --c++23: + + int x = auto{[](int a) { return a; }}(2); // "class __T1{...}(2)" + int f() { + const auto c = [n = 0]() mutable { return ++n; }; + return auto{c}(); // Call of a const object. + } + +The new is_auto_cast field of a dynamic initialization now records such a +cast, and it is 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, a typedef +was put out inside the declaration, as in + + if (auto m = []{ return 1; } typedef decltype(m) __T2;) ... + +No typedef is put out for such a variable now. 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..8d3f4ee2fb --- /dev/null +++ b/tests/tests/changes/GH576.sft.cpp @@ -0,0 +1,97 @@ +//type:fp +//options:--c++23 +//remark:auto casts and closure-type typedefs in generated C++ +// +// In generated C++, an auto 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. An auto cast of a const closure variable used the +// typedef put out for the variable's type, which is const. 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(); + }(); +} + +int f6() { + const auto c = [n = 0]() mutable { return ++n; }; + return auto{c}() + auto(c)(); +} + +struct B { int v; B(int); B(const B&); int operator()(int) const; }; +B b(1); +int u = auto{b}(1) + auto(b)(2) + auto{B(i)}(3) + auto(B{i})(4); + +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; +}