aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2019-06-12 22:41:35 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2019-06-12 22:41:35 +0000
commit22f6d174419e0649315049f5e2aec4b9a90e6ddc (patch)
tree9f88d2baf98fe2d850f45921a783813d7575ec0e /gcc
parent0d0137a37fc29c1dc94bb8f8f90b5fdece6cb64b (diff)
downloadgcc-22f6d174419e0649315049f5e2aec4b9a90e6ddc.zip
gcc-22f6d174419e0649315049f5e2aec4b9a90e6ddc.tar.gz
gcc-22f6d174419e0649315049f5e2aec4b9a90e6ddc.tar.bz2
PR c++/66999 - 'this' captured by reference.
* parser.c (cp_parser_lambda_introducer): Reject `&this'. Use cp_lexer_nth_token_is instead of cp_lexer_peek_nth_token. * g++.dg/cpp0x/lambda/lambda-this21.C: New test. From-SVN: r272223
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog4
-rw-r--r--gcc/cp/parser.c14
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this21.C10
4 files changed, 31 insertions, 2 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 87ca00b..37d2b2a 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,9 @@
2019-06-12 Marek Polacek <polacek@redhat.com>
+ PR c++/66999 - 'this' captured by reference.
+ * parser.c (cp_parser_lambda_introducer): Reject `&this'. Use
+ cp_lexer_nth_token_is instead of cp_lexer_peek_nth_token.
+
PR c++/90825 - endless recursion when evaluating sizeof.
PR c++/90832 - endless recursion when evaluating sizeof.
* constexpr.c (cxx_eval_constant_expression): Don't recurse on the
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index e699fbc..8f5ae84 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -10526,7 +10526,8 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
/* Record default capture mode. "[&" "[=" "[&," "[=," */
if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
- && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
+ && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
+ && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
@@ -10609,6 +10610,17 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
continue;
}
+ /* But reject `&this'. */
+ if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
+ && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
+ {
+ error_at (cp_lexer_peek_token (parser->lexer)->location,
+ "%<this%> cannot be captured by reference");
+ cp_lexer_consume_token (parser->lexer);
+ cp_lexer_consume_token (parser->lexer);
+ continue;
+ }
+
bool init_pack_expansion = false;
location_t ellipsis_loc = UNKNOWN_LOCATION;
if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 0580273..1c49ab9 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,4 +1,7 @@
-2019-06-11 Marek Polacek <polacek@redhat.com>
+2019-06-12 Marek Polacek <polacek@redhat.com>
+
+ PR c++/66999 - 'this' captured by reference.
+ * g++.dg/cpp0x/lambda/lambda-this21.C: New test.
PR c++/90825 - endless recursion when evaluating sizeof.
PR c++/90832 - endless recursion when evaluating sizeof.
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this21.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this21.C
new file mode 100644
index 0000000..538dd37
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this21.C
@@ -0,0 +1,10 @@
+// PR c++/66999 - 'this' captured by reference.
+// { dg-do compile { target c++11 } }
+
+struct X {
+ void bar (int n)
+ {
+ auto l1 = [&this] { }; // { dg-error ".this. cannot be captured by reference" }
+ auto l2 = [=, &this] { }; // { dg-error ".this. cannot be captured by reference" }
+ }
+};