From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 49470385842D; Wed, 15 Dec 2021 19:55:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 49470385842D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-9391] c++: redundant explicit 'this' capture before C++20 [PR100493] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 648d5aa56abd2a0ab36403dd1e14dd225402301a X-Git-Newrev: d33f68865f6f71c28cfee5dfd91765e86d471ef1 Message-Id: <20211215195536.49470385842D@sourceware.org> Date: Wed, 15 Dec 2021 19:55:36 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Dec 2021 19:55:36 -0000 https://gcc.gnu.org/g:d33f68865f6f71c28cfee5dfd91765e86d471ef1 commit r11-9391-gd33f68865f6f71c28cfee5dfd91765e86d471ef1 Author: Patrick Palka Date: Mon Nov 29 07:52:47 2021 -0500 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. (cherry picked from commit 1420ff3efcff98df0e8c6f021a7ff24b5fc65043) Diff: --- gcc/cp/parser.c | 8 +++++--- gcc/testsuite/g++.dg/cpp1z/lambda-this1.C | 8 ++++---- gcc/testsuite/g++.dg/cpp1z/lambda-this8.C | 9 +++++++++ gcc/testsuite/g++.dg/cpp2a/lambda-this3.C | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 8aac2d1803c..af10caa48be 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -11023,10 +11023,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 % redundant " - "with by-copy capture default"); + pedwarn (loc, 0, + "explicit by-copy capture of % " + "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, diff --git a/gcc/testsuite/g++.dg/cpp1z/lambda-this1.C b/gcc/testsuite/g++.dg/cpp1z/lambda-this1.C index b13ff8b9fc6..e12330a8291 100644 --- a/gcc/testsuite/g++.dg/cpp1z/lambda-this1.C +++ b/gcc/testsuite/g++.dg/cpp1z/lambda-this1.C @@ -18,7 +18,7 @@ struct A { auto i = [=] { return a; }; // { dg-warning "implicit capture" "" { target c++2a } } auto j = [&] { return a; }; // P0409R2 - C++2A lambda capture [=, this] - auto k = [=, this] { return a; };// { dg-error "explicit by-copy capture of 'this' redundant with by-copy capture default" "" { target c++17_down } } + auto k = [=, this] { return a; };// { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } } auto l = [&, this] { return a; }; auto m = [=, *this] { return a; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } auto n = [&, *this] { return a; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } @@ -27,12 +27,12 @@ struct A { // { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } auto q = [=, this, *this] { return a; };// { dg-error "already captured 'this'" } // { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } - // { dg-error "explicit by-copy capture of 'this' redundant with by-copy capture default" "" { target c++17_down } .-2 } + // { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-2 } auto r = [=, this, this] { return a; };// { dg-error "already captured 'this'" } - // { dg-error "explicit by-copy capture of 'this' redundant with by-copy capture default" "" { target c++17_down } .-1 } + // { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-1 } auto s = [=, *this, this] { return a; };// { dg-error "already captured 'this'" } // { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } - // { dg-error "explicit by-copy capture of 'this' redundant with by-copy capture default" "" { target c++17_down } .-2 } + // { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-2 } auto t = [=, *this, *this] { return a; };// { dg-error "already captured 'this'" } // { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } auto u = [&, this, *this] { return a; };// { dg-error "already captured 'this'" } diff --git a/gcc/testsuite/g++.dg/cpp1z/lambda-this8.C b/gcc/testsuite/g++.dg/cpp1z/lambda-this8.C new file mode 100644 index 00000000000..4542cbd8506 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/lambda-this8.C @@ -0,0 +1,9 @@ +// PR c++/100493 +// { dg-do compile { target c++17 } } +// { dg-options "" } + +struct A { + void f() { + [=, this] { }; + } +}; diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C b/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C index 3e00e68e906..bc54a4c895d 100644 --- a/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C @@ -1,6 +1,6 @@ // P0806R2 // { dg-do compile { target c++17 } } -// { dg-options "" } +// { dg-options "-pedantic" } struct X { int x;