From: Patrick Palka Date: Fri, 29 May 2020 13:40:40 +0000 (-0400) Subject: c++: lambdas inside constraints [PR92652] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=020d86db8896f088435830595640e6fc21bc64ad;p=gcc.git 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. Passes 'make check-c++', and also tested by building cmcstl2, does this look OK to commit after a full bootstrap/regtest? 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. --- diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 54ca875ce54..74c40efd936 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -27663,11 +27663,16 @@ static tree cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p) { processing_constraint_expression_sentinel parsing_constraint; - ++processing_template_decl; + temp_override 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); if (check_for_bare_parameter_packs (expr)) expr = error_mark_node; - --processing_template_decl; return expr; } @@ -27684,12 +27689,14 @@ static tree cp_parser_constraint_expression (cp_parser *parser) { processing_constraint_expression_sentinel parsing_constraint; - ++processing_template_decl; + temp_override 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, PREC_NOT_OPERATOR, NULL); if (check_for_bare_parameter_packs (expr)) expr = error_mark_node; - --processing_template_decl; expr.maybe_add_location_wrapper (); return expr; } @@ -27798,9 +27805,11 @@ cp_parser_requires_expression (cp_parser *parser) parms = NULL_TREE; /* Parse the requirement body. */ - ++processing_template_decl; + temp_override 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); - --processing_template_decl; if (reqs == error_mark_node) return error_mark_node; } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C new file mode 100644 index 00000000000..392da312b28 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C @@ -0,0 +1,7 @@ +// PR c++/94128 +// { dg-do compile { target c++20 } } + +void test(auto param) +requires requires{ { [](auto p){return p;}(param) }; }; + +void test2() { test(1); } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C new file mode 100644 index 00000000000..c1c9be682d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C @@ -0,0 +1,11 @@ +// PR c++/92652 +// { dg-do compile { target concepts } } + +template < typename T > + requires ([]{return true ;}()) +void h() { } + +int main() +{ + h(); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C new file mode 100644 index 00000000000..6b81ba0adac --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C @@ -0,0 +1,11 @@ +// PR c++/93698 +// { dg-do compile { target concepts } } + +#include + +template +concept foo = [](std::index_sequence) constexpr { + return (Is + ...) > 10; +}(std::make_index_sequence()); + +bool a = foo<7>;