From patchwork Fri Nov 19 19:25:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 47949 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C28CD385AC12 for ; Fri, 19 Nov 2021 19:25:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C28CD385AC12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637349936; bh=/uR2MELlDaqNda2xqgtIbMeVSYDUmWkmNOPTKgAa0cs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yYykX3KKSL+1zMgm5xeTXngCj1x4vLUa6IBt/nXgT5fK90YzTZM/STM60N7hgIy7+ MzULNliHpPnPhJGULh9v6w13/6pcbFsLX2qFmRvbZ9WG0773cBqP02xfZvUcSDw2Vr we1c7r05gxhzyn52UDAyST77UTSOan20XSr0d/pI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7B15B385840D for ; Fri, 19 Nov 2021 19:25:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B15B385840D Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-216-euI2vMppPp-ikYGs9M-z2g-1; Fri, 19 Nov 2021 14:25:04 -0500 X-MC-Unique: euI2vMppPp-ikYGs9M-z2g-1 Received: by mail-qt1-f199.google.com with SMTP id w12-20020ac80ecc000000b002a7a4cd22faso7727404qti.4 for ; Fri, 19 Nov 2021 11:25:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/uR2MELlDaqNda2xqgtIbMeVSYDUmWkmNOPTKgAa0cs=; b=XFF/r8dwqAKtztNmBm+a7Ta3DFHbrslUsZb7h5eY874UKKy1J9lHspCXQE+GZPLROJ xZYV7G6mBLupdN6s8xIGBlvmHhHoQLD6UctOIz+4GYcpap6szFrS9iCJrtyvy5LQCMo2 cnAk5ahXyS9yXok55GipaA+knJgebUuoMyQbno/njjIglDZOh3C4ASamFjV3eLraASPW ZAI/04nSTsxgKYafk7hj77xzQZbtodZ8ygFN5PP746AlBZSqdNg0f4v9ORWVetZnOPAN dZhKSNCn9ZuCCt8cRAgA7o1CTiW3ZqOHV4DQ6qUkQPMGDciONiy9Uv9gJb2GQEYSHJCG RZQQ== X-Gm-Message-State: AOAM532sFli1KYjXcn9mJKgMbDN6aDmrKxOTNWu4neeKDgnzGW/N3aKB jvfb5uk3ar5bWnFGxKpIs+s/+p1nCLVAEc4H4mMTmwuh+4MEGgJgqk4O4/4dEFkrmTWZR8KeF0Q /JBjopT38uaAssGuKBvwjnHwFDb+OCGfp9izMNP6JAaMLUWyyI/UQaj/eJK5KOGybnz8= X-Received: by 2002:a05:620a:4f2:: with SMTP id b18mr30873905qkh.310.1637349903376; Fri, 19 Nov 2021 11:25:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJwD52sPE5BcToO7nJQqy1S2L/TICcEI0kN/lgrHi3wp81hu8kUzUnDLd3Lx4EofmB1USZrahQ== X-Received: by 2002:a05:620a:4f2:: with SMTP id b18mr30873867qkh.310.1637349903008; Fri, 19 Nov 2021 11:25:03 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id u11sm283964qko.119.2021.11.19.11.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 11:25:02 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: redundant explicit 'this' capture in C++17 [PR100493] Date: Fri, 19 Nov 2021 14:25:01 -0500 Message-Id: <20211119192501.2205789-1-ppalka@redhat.com> X-Mailer: git-send-email 2.34.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" As described in detail in the PR, in C++20 implicitly capturing 'this' via the '=' capture default is deprecated, but in C++17 explicitly capturing 'this' alongside a '=' capture default is ill-formed. This means it's impossible to write a C++17 lambda that captures 'this' and that also has a '=' capture default in a forward-compatible way with C++20: [=] { 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 mention that the construct in question is a C++20 one. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/11? 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 improve the wording. 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. --- gcc/cp/parser.c | 8 +++++--- gcc/testsuite/g++.dg/cpp1z/lambda-this1.C | 8 ++++---- gcc/testsuite/g++.dg/cpp1z/lambda-this8.C | 10 ++++++++++ gcc/testsuite/g++.dg/cpp2a/lambda-this3.C | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/lambda-this8.C diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 65f0f112011..30790006ac9 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -11120,10 +11120,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, OPT_Wc__20_extensions, + "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..b87ae0af62a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/lambda-this8.C @@ -0,0 +1,10 @@ +// PR c++/100493 +// { dg-do compile { target c++17 } } +// { dg-options "" } + +struct A { + int m; + 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;