diff --git a/src/cp_gen_be.c b/src/cp_gen_be.c index 52e8dd0a3b..1fa2437ae1 100644 --- a/src/cp_gen_be.c +++ b/src/cp_gen_be.c @@ -2939,6 +2939,11 @@ that block. if (opnd != NULL && !expr_is_unusable(opnd)) { is_accessible = TRUE; } /* if */ + } else if (kind == iek_type && is_immediate_class_type((a_type_ptr)scp) && + class_type_supp((a_type_ptr)scp)->is_lambda_closure_class && + !((a_type_ptr)scp)->has_been_declared) { + /* A closure type with no emitted typedef has no name in the output. */ + local_for_all_scopes = FALSE; } else if (scp->access == as_public) { /* Either a public class member or a non-member. */ if (parent_class != NULL) { @@ -13240,9 +13245,17 @@ to unusable variables and class members. kind = iek_variable; break; case enk_routine: - scp = &node_routine(expr)->source_corresp; + { + a_routine_ptr rp = node_routine(expr); + a_name_reference_ptr nrp = name_ref_for_node(expr); + a_template_arg_ptr tap = NULL; + long num_args = -1; + a_boolean args_public = TRUE; + + if (rp == NULL) break; + scp = &rp->source_corresp; kind = iek_routine; - if (scp != NULL && scp->is_class_member && scp->is_local_to_function && + if (scp->is_class_member && scp->is_local_to_function && class_type_supp(scp_parent_class(scp))->is_lambda_closure_class && closure_is_in_scope(scp_parent_class(scp))) { /* This is the call operator of a local lambda appearing in the @@ -13250,7 +13263,31 @@ to unusable variables and class members. operator can be referenced. */ is_local_lambda_in_scope = TRUE; } /* if */ + if (nrp != NULL) { + num_args = nrp->is_template_id ? nrp->num_template_arguments : 0; + if (nrp->is_template_id) tap = nrp->orig_template_arg_list; + } /* if */ + if (tap == NULL && num_args != 0) { + (void)name_has_template_arguments(scp, kind, &tap, + /*param_ptr=*/NULL, + /*insert_space=*/NULL); + } /* if */ + /* A substituted type operator can refer to an unnameable type through + a function's explicit template arguments. */ + begin_template_arg_list_traversal_simple(tap, &tap); + for (long i = 0; tap != NULL && i != num_args; + ++i, advance_to_next_template_arg_simple(&tap)) { + if (!template_arg_is_accessible(tap, /*ignore_context=*/FALSE, + /*check_related_types=*/TRUE, + &args_public, + /*check_visibility=*/TRUE)) { + tblock->result = TRUE; + tblock->terminate = TRUE; + goto done; + } /* if */ + } /* for */ break; + } case enk_field: scp = &node_field(expr)->source_corresp; kind = iek_field; @@ -13444,6 +13481,7 @@ to unusable variables and class members. tblock->result = TRUE; tblock->terminate = TRUE; } /* if */ +done:; } /* check_for_unusable_entity */ @@ -13458,14 +13496,20 @@ reference, so check for that also as a special case. */ { an_expr_or_stmt_traversal_block tblock; + a_member_access_operand *saved_pending = + pending_member_access_operands; clear_expr_or_stmt_traversal_block(&tblock); tblock.process_expr = check_for_unusable_entity; tblock.process_non_dynamic_constants = TRUE; tblock.process_expressions_for_constants = TRUE; tblock.process_template_parameter_constants_and_expressions = TRUE; - pop_member_access_operand_stack(/*pop_all=*/TRUE); + /* A nested check (e.g., for a decltype template argument) must have its + own stack of member access operands. */ + pending_member_access_operands = NULL; traverse_expr(expr, &tblock); + pop_member_access_operand_stack(/*pop_all=*/TRUE); + pending_member_access_operands = saved_pending; return tblock.result; } /* expr_is_unusable */ diff --git a/src/pending_changes/gh-586-unnamed-lambda-cast.txt b/src/pending_changes/gh-586-unnamed-lambda-cast.txt new file mode 100644 index 0000000000..c49ff4fe65 --- /dev/null +++ b/src/pending_changes/gh-586-unnamed-lambda-cast.txt @@ -0,0 +1,20 @@ +[GH #586] +C++-generating back end: reference casts involving unnamed lambda types + +The changes for EDGcpfe/26294 in version 6.8 could cause the C++-generating +back end to emit an unnameable lambda closure type as a template argument +in the destination type of a reference cast. For example, with --c++17: + + template T&& declval(); + template using value_t = decltype(declval()()); + template struct R { T value; }; + template R> make(F fn) { return {fn()}; } + + int f() { + auto&& r = make([] { return 1; }); + return static_cast(r).value; + } + +Previously, the generated cast used value_t with a synthetic name for the +lambda's closure type, which made the generated code uncompilable. The +back end now emits R&& as the destination type in this example. diff --git a/tests/tests/regressions/.unnamed-lambda-cast.rto/default.1.1.txt b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.1.1.txt new file mode 100644 index 0000000000..3750f7eec7 --- /dev/null +++ b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.1.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=1 --c++17 Test_name.c diff --git a/tests/tests/regressions/.unnamed-lambda-cast.rto/default.2.1.txt b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.2.1.txt new file mode 100644 index 0000000000..f225549b14 --- /dev/null +++ b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.2.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=2 --c++17 --gnu_version 150200 Test_name.c diff --git a/tests/tests/regressions/.unnamed-lambda-cast.rto/default.3.1.txt b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.3.1.txt new file mode 100644 index 0000000000..21f8a97916 --- /dev/null +++ b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.3.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=3 --c++17 --clang_version 230100 Test_name.c diff --git a/tests/tests/regressions/.unnamed-lambda-cast.rto/default.4.1.txt b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.4.1.txt new file mode 100644 index 0000000000..b07e4ec4b1 --- /dev/null +++ b/tests/tests/regressions/.unnamed-lambda-cast.rto/default.4.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=4 --ms_c++17 --microsoft_version 1950 Test_name.c diff --git a/tests/tests/regressions/unnamed-lambda-cast.sft.cpp b/tests/tests/regressions/unnamed-lambda-cast.sft.cpp new file mode 100644 index 0000000000..193e7b5709 --- /dev/null +++ b/tests/tests/regressions/unnamed-lambda-cast.sft.cpp @@ -0,0 +1,48 @@ +//type:cp +//options:--c++17:--c++17 --gnu_version 150200:--c++17 --clang_version 230100:--ms_c++17 --microsoft_version 1950 +//remark:Reconstruct a cast whose deduced type involves an unnamed lambda + +template T&& declval(); + +template struct identity { + using type = T; +}; + +template struct invoke_result { + // Expanding invoke_result_t can expose this decltype. After + // substitution, G is the closure type, so emitting declval() would + // still require a name for it. The back end must use the resolved int. + template + static identity()(0))> test(int); + using type = typename decltype(test(0))::type; +}; + +template using invoke_result_t = typename invoke_result::type; + +template struct result { + T value; +}; + +template result> transform(int input, F&& f) { + return {f(input)}; +} + +int f(int input) { + // Passing the lambda directly leaves its closure type without a name in + // the generated C++. When reconstructing the cast, the back end must + // expand invoke_result_t, whose template argument is that closure type. + auto&& r = transform(input, [](auto x) { return x + 1; }); + // The reconstructed destination type is result&&. + return static_cast(r).value; +} + +auto make_callable() { + struct local {}; + return [](auto) { return local{}; }; +} + +auto callable = make_callable(); +// Checking the inner decltype must preserve the member-access exemption +// for the call. The outer decltype is usable through callable, and its +// resolved type local cannot be named here. +decltype(callable.operator()(0)) value;