Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -13240,17 +13245,49 @@ 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
current function. The closure type is in scope and its call
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)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
tblock->result = TRUE;
tblock->terminate = TRUE;
goto done;
} /* if */
} /* for */
break;
}
case enk_field:
scp = &node_field(expr)->source_corresp;
kind = iek_field;
Expand Down Expand Up @@ -13444,6 +13481,7 @@ to unusable variables and class members.
tblock->result = TRUE;
tblock->terminate = TRUE;
} /* if */
done:;
} /* check_for_unusable_entity */


Expand All @@ -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 */

Expand Down
20 changes: 20 additions & 0 deletions src/pending_changes/gh-586-unnamed-lambda-cast.txt
Original file line number Diff line number Diff line change
@@ -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<class T> T&& declval();
template<class F> using value_t = decltype(declval<F>()());
template<class T> struct R { T value; };
template<class F> R<value_t<F>> make(F fn) { return {fn()}; }

int f() {
auto&& r = make([] { return 1; });
return static_cast<decltype(r)&&>(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<int>&& as the destination type in this example.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=1 --c++17 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=2 --c++17 --gnu_version 150200 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=3 --c++17 --clang_version 230100 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=4 --ms_c++17 --microsoft_version 1950 Test_name.c
48 changes: 48 additions & 0 deletions tests/tests/regressions/unnamed-lambda-cast.sft.cpp
Original file line number Diff line number Diff line change
@@ -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<typename T> T&& declval();

template<typename T> struct identity {
using type = T;
};

template<typename F> struct invoke_result {
// Expanding invoke_result_t<F> can expose this decltype. After
// substitution, G is the closure type, so emitting declval<G>() would
// still require a name for it. The back end must use the resolved int.
template<typename G>
static identity<decltype(declval<G>()(0))> test(int);
using type = typename decltype(test<F>(0))::type;
};

template<typename F> using invoke_result_t = typename invoke_result<F>::type;

template<typename T> struct result {
T value;
};

template<typename F> result<invoke_result_t<F>> 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<F>, whose template argument is that closure type.
auto&& r = transform(input, [](auto x) { return x + 1; });
// The reconstructed destination type is result<int>&&.
return static_cast<decltype(r)&&>(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()<decltype(0)>(0)) value;
Loading