From 774fb6c4eb205eaf9d3b6667e7de9c90cc1784ad Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 21 Aug 2018 18:37:23 +0000 Subject: [PATCH] re PR c++/86499 (lambda-expressions with capture-default are allowed at namespace scope) 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. From-SVN: r263749 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/parser.c | 3 +++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C | 10 ++++++++++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C | 2 +- 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 29f3fd86466..0fbd8167a04 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-08-21 Marek Polacek + + PR c++/86499 + * parser.c (cp_parser_lambda_introducer): Give error if a non-local + lambda has a capture-default. + 2018-08-21 Paolo Carlini * decl.c (check_static_variable_definition): Change to return void. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index aa5286b24cc..49d476b383f 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -10285,6 +10285,9 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) { cp_lexer_consume_token (parser->lexer); first = false; + + if (!(at_function_scope_p () || parsing_nsdmi ())) + error ("non-local lambda expression cannot have a capture-default"); } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6c87f8017d3..b5ba160b6b3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -13,6 +13,10 @@ * g++.dg/cpp0x/Wnarrowing12.C: New test. * g++.dg/cpp0x/rv-cast5.C: Add static_cast. + PR c++/86499 + * g++.dg/cpp0x/lambda/lambda-non-local.C: New test. + * g++.dg/cpp0x/lambda/lambda-this10.C: Adjust dg-error. + 2018-08-21 Ed Schonberg * gnat.dg/expr_func7.adb, gnat.dg/expr_func7.ads: New testcase. diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-non-local.C new file mode 100644 index 00000000000..005f8f3888b --- /dev/null +++ b/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 a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C index b4b8e7201aa..e8e94771ef0 100644 --- a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this10.C +++ b/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" } -- 2.30.2