diff options
author | Patrick Palka <ppalka@redhat.com> | 2021-11-29 07:52:47 -0500 |
---|---|---|
committer | Patrick Palka <ppalka@redhat.com> | 2021-11-29 07:52:47 -0500 |
commit | 1420ff3efcff98df0e8c6f021a7ff24b5fc65043 (patch) | |
tree | 692094210fd905557b78e08063f1f0e55bb25e15 /gcc/cp/parser.c | |
parent | a5d269f0c1cda545a86da960e8989bea862dd75e (diff) | |
download | gcc-1420ff3efcff98df0e8c6f021a7ff24b5fc65043.zip gcc-1420ff3efcff98df0e8c6f021a7ff24b5fc65043.tar.gz gcc-1420ff3efcff98df0e8c6f021a7ff24b5fc65043.tar.bz2 |
c++: redundant explicit 'this' capture before C++20 [PR100493]
As described in detail in the PR, in C++20 implicitly capturing 'this'
via a '=' capture default is deprecated, and in C++17 adding an explicit
'this' capture alongside a '=' capture default is diagnosed as redundant
(and is strictly speaking ill-formed). This means it's impossible to
write, in a forward-compatible way, a C++17 lambda that has a '=' capture
default and that also captures 'this' (implicitly or explicitly):
[=] { this; } // #1 deprecated in C++20, OK in C++17
// GCC issues a -Wdeprecated warning in C++20 mode
[=, this] { } // #2 ill-formed in C++17, OK in C++20
// GCC issues an unconditional warning in C++17 mode
This patch resolves this dilemma by downgrading the warning for #2 into
a -pedantic one. In passing, move it into the -Wc++20-extensions class
of warnings and adjust its wording accordingly.
PR c++/100493
gcc/cp/ChangeLog:
* parser.c (cp_parser_lambda_introducer): In C++17, don't
diagnose a redundant 'this' capture alongside a by-copy
capture default unless -pedantic. Move the diagnostic into
-Wc++20-extensions and adjust wording accordingly.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1z/lambda-this1.C: Adjust expected diagnostics.
* g++.dg/cpp1z/lambda-this8.C: New test.
* g++.dg/cpp2a/lambda-this3.C: Compile with -pedantic in C++17
to continue to diagnose redundant 'this' captures.
Diffstat (limited to 'gcc/cp/parser.c')
-rw-r--r-- | gcc/cp/parser.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 0bd5852..899797b 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -11184,10 +11184,12 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS)) { location_t loc = cp_lexer_peek_token (parser->lexer)->location; - if (cxx_dialect < cxx20 + if (cxx_dialect < cxx20 && pedantic && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY) - pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant " - "with by-copy capture default"); + pedwarn (loc, OPT_Wc__20_extensions, + "explicit by-copy capture of %<this%> " + "with by-copy capture default only available with " + "%<-std=c++20%> or %<-std=gnu++20%>"); cp_lexer_consume_token (parser->lexer); if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr)) pedwarn (input_location, 0, |