Commit 03c344ad by Patrick Palka

c++: lambdas inside constraints [PR92652]

When parsing a constraint-expression, a requires-clause or a
requires-expression, we temporarily increment processing_template_decl
so that we always obtain template trees which we could later reduce via
substitution even when not inside a template.

But incrementing processing_template_decl when we're already inside a
template has the unintended side effect of shifting up the template
parameter levels of a lambda defined inside one of these constructs,
which leads to confusion later during substitution into the lambda.

This patch fixes this issue by incrementing processing_template_decl
during parsing of these constructs only if it is 0.

gcc/cp/ChangeLog:

	PR c++/92652
	PR c++/93698
	PR c++/94128
	* parser.c (cp_parser_requires_clause_expression): Temporarily
	increment processing_template_decl only if it is 0.
	(cp_parser_constraint_expression): Likewise.
	(cp_parser_requires_expression): Likewise.

gcc/testsuite/ChangeLog:

	PR c++/92652
	PR c++/93698
	PR c++/94128
	* g++.dg/cpp2a/concepts-lambda8.C: New test.
	* g++.dg/cpp2a/concepts-lambda9.C: New test.
	* g++.dg/cpp2a/concepts-lambda10.C: New test.

(cherry picked from commit 020d86db8896f088435830595640e6fc21bc64ad)
parent 798a9da4
...@@ -27646,11 +27646,16 @@ static tree ...@@ -27646,11 +27646,16 @@ static tree
cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p) cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
{ {
processing_constraint_expression_sentinel parsing_constraint; processing_constraint_expression_sentinel parsing_constraint;
++processing_template_decl; temp_override<int> ovr (processing_template_decl);
if (!processing_template_decl)
/* Adjust processing_template_decl so that we always obtain template
trees here. We don't do the usual ++processing_template_decl
because that would skew the template parameter depth of a lambda
within if we're already inside a template. */
processing_template_decl = 1;
cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p); cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
if (check_for_bare_parameter_packs (expr)) if (check_for_bare_parameter_packs (expr))
expr = error_mark_node; expr = error_mark_node;
--processing_template_decl;
return expr; return expr;
} }
...@@ -27667,12 +27672,14 @@ static tree ...@@ -27667,12 +27672,14 @@ static tree
cp_parser_constraint_expression (cp_parser *parser) cp_parser_constraint_expression (cp_parser *parser)
{ {
processing_constraint_expression_sentinel parsing_constraint; processing_constraint_expression_sentinel parsing_constraint;
++processing_template_decl; temp_override<int> ovr (processing_template_decl);
if (!processing_template_decl)
/* As in cp_parser_requires_clause_expression. */
processing_template_decl = 1;
cp_expr expr = cp_parser_binary_expression (parser, false, true, cp_expr expr = cp_parser_binary_expression (parser, false, true,
PREC_NOT_OPERATOR, NULL); PREC_NOT_OPERATOR, NULL);
if (check_for_bare_parameter_packs (expr)) if (check_for_bare_parameter_packs (expr))
expr = error_mark_node; expr = error_mark_node;
--processing_template_decl;
expr.maybe_add_location_wrapper (); expr.maybe_add_location_wrapper ();
return expr; return expr;
} }
...@@ -27781,9 +27788,11 @@ cp_parser_requires_expression (cp_parser *parser) ...@@ -27781,9 +27788,11 @@ cp_parser_requires_expression (cp_parser *parser)
parms = NULL_TREE; parms = NULL_TREE;
/* Parse the requirement body. */ /* Parse the requirement body. */
++processing_template_decl; temp_override<int> ovr (processing_template_decl);
if (!processing_template_decl)
/* As in cp_parser_requires_clause_expression. */
processing_template_decl = 1;
reqs = cp_parser_requirement_body (parser); reqs = cp_parser_requirement_body (parser);
--processing_template_decl;
if (reqs == error_mark_node) if (reqs == error_mark_node)
return error_mark_node; return error_mark_node;
} }
// PR c++/94128
// { dg-do compile { target c++20 } }
void test(auto param)
requires requires{ { [](auto p){return p;}(param) }; };
void test2() { test(1); }
// PR c++/92652
// { dg-do compile { target concepts } }
template < typename T >
requires ([]{return true ;}())
void h() { }
int main()
{
h<int>();
}
// PR c++/93698
// { dg-do compile { target concepts } }
#include <utility>
template <int N>
concept foo = []<std::size_t... Is>(std::index_sequence<Is...>) constexpr {
return (Is + ...) > 10;
}(std::make_index_sequence<N>());
bool a = foo<7>;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment