This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for c++/86499, non-local lambda with a capture-default
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>, Jason Merrill <jason at redhat dot com>
- Date: Mon, 13 Aug 2018 14:50:28 -0400
- Subject: C++ PATCH for c++/86499, non-local lambda with a capture-default
This PR complains about us accepting a lambda with a capture-default
at file scope, which seems to be forbidden. This patch disallows
such a use. clang gives an error, so I did the same, but maybe we
only want a pedwarn.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2018-08-13 Marek Polacek <polacek@redhat.com>
PR c++/86499
* parser.c (cp_parser_lambda_introducer): Give error if a non-local
lambda has a capture-default.
* g++.dg/cpp0x/lambda/lambda-non-local.C: New test.
* g++.dg/cpp0x/lambda/lambda-this10.C: Adjust dg-error.
diff --git gcc/cp/parser.c gcc/cp/parser.c
index 8cfcd150705..6a760136f99 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -10280,6 +10280,9 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
{
cp_lexer_consume_token (parser->lexer);
first = false;
+
+ if (!(at_function_scope_p () || at_class_scope_p ()))
+ error ("non-local lambda expression cannot have a capture-default");
}
while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
diff --git gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C
index e69de29bb2d..005f8f3888b 100644
--- gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C
+++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C
@@ -0,0 +1,10 @@
+// PR c++/86499
+// { dg-do compile { target c++11 } }
+
+auto l1 = [=]{}; // { dg-error "non-local lambda expression cannot have a capture-default" }
+auto l2 = [&]{}; // { dg-error "non-local lambda expression cannot have a capture-default" }
+
+namespace {
+ auto l3 = [=]{}; // { dg-error "non-local lambda expression cannot have a capture-default" }
+ auto l4 = [&]{}; // { dg-error "non-local lambda expression cannot have a capture-default" }
+}
diff --git gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C
index b4b8e7201aa..e8e94771ef0 100644
--- gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C
+++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C
@@ -1,4 +1,4 @@
// PR c++/54383
// { dg-do compile { target c++11 } }
-auto foo = [&](int a) { return a > this->b; }; // { dg-error "this" }
+auto foo = [&](int a) { return a > this->b; }; // { dg-error "non-local|this" }