From patchwork Sun Aug 12 03:53:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 956648 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-483535-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="l0giVREd"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41p4j30kVBz9s7X for ; Sun, 12 Aug 2018 13:53:40 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=IKFYrtQ8jF9wYmkZS6l0q0tVNPr60XQdgwhx3ioFMVQm31FKlVXeO nXehYZZ1NB1qjb2XqlFLRdhEgBwAdkIcunI0j8N4luPVcaXQ6hgm9FMh+eA45zL5 TNyk08HOh4M/ZW4XUyyZqwy6O8o8/jrkJqwp855RF7dOUc3EgWUx2w= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=ldAJkz1ZpFmIhndMYvA6rqLi5ME=; b=l0giVREdre/W6fvtdcdV Rn3uQOnkHJ+ZSYVchgEvjaxB10onqXV3wdf00dkiW/dZSWBO/KPk/YrLi2+pz+io cYuj9+l4rjSckwXvowQ99RkAmx6r4DSKGMyNqk8axfEsAEzAGmbscbgOu7eNQ0ye NIQFwhUzgQu47/Ro90AVVY0= Received: (qmail 12895 invoked by alias); 12 Aug 2018 03:53:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 12874 invoked by uid 89); 12 Aug 2018 03:53:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=cpp2a, Formatting X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 12 Aug 2018 03:53:28 +0000 Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 633CE8F01F for ; Sun, 12 Aug 2018 03:53:27 +0000 (UTC) Received: from redhat.com (ovpn-121-137.rdu2.redhat.com [10.10.121.137]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E5652309128B; Sun, 12 Aug 2018 03:53:26 +0000 (UTC) Date: Sat, 11 Aug 2018 23:53:20 -0400 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH to implement C++20 P0806R2, Deprecate implicit capture of this via [=] Message-ID: <20180812035319.GN4317@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) When we capture the current object (*this) implicitly, it is always captured by reference, even if the capture default is =. This is surprising and irregular so this proposal deprecates implicit capture of this via [=] (not via [&]). Users should now use [=, this] (same as [=], added by P0409R2), or [=, *this]. This patch adds a -Wdeprecated warning to address it. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-08-11 Marek Polacek P0806R2 - Deprecate implicit capture of this via [=] * lambda.c (add_default_capture): Formatting fixes. Warn about deprecated implicit capture of this via [=]. * g++.dg/cpp2a/lambda-this1.C: New test. * g++.dg/cpp2a/lambda-this2.C: New test. * g++.dg/cpp2a/lambda-this3.C: New test. --- gcc/cp/lambda.c +++ gcc/cp/lambda.c @@ -695,14 +695,10 @@ tree add_default_capture (tree lambda_stack, tree id, tree initializer) { bool this_capture_p = (id == this_identifier); - tree var = NULL_TREE; - tree saved_class_type = current_class_type; - tree node; - - for (node = lambda_stack; + for (tree node = lambda_stack; node; node = TREE_CHAIN (node)) { @@ -711,15 +707,26 @@ add_default_capture (tree lambda_stack, tree id, tree initializer) current_class_type = LAMBDA_EXPR_CLOSURE (lambda); if (DECL_PACK_P (initializer)) initializer = make_pack_expansion (initializer); - var = add_capture (lambda, - id, - initializer, - /*by_reference_p=*/ - (this_capture_p - || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) - == CPLD_REFERENCE)), - /*explicit_init_p=*/false); + var = add_capture (lambda, id, initializer, + /*by_reference_p=*/ + (this_capture_p + || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) + == CPLD_REFERENCE)), + /*explicit_init_p=*/false); initializer = convert_from_reference (var); + + /* Warn about deprecated implicit capture of this via [=]. */ + if (cxx_dialect >= cxx2a + && this_capture_p + && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_COPY + && !in_system_header_at (LAMBDA_EXPR_LOCATION (lambda))) + { + if (warning_at (LAMBDA_EXPR_LOCATION (lambda), OPT_Wdeprecated, + "implicit capture of %qE via %<[=]%> is deprecated " + "in C++20", this_identifier)) + inform (LAMBDA_EXPR_LOCATION (lambda), "add explicit % or " + "%<*this%> capture"); + } } current_class_type = saved_class_type; --- gcc/testsuite/g++.dg/cpp2a/lambda-this1.C +++ gcc/testsuite/g++.dg/cpp2a/lambda-this1.C @@ -0,0 +1,51 @@ +// P0806R2 +// { dg-do compile } +// { dg-options "-std=c++2a" } + +struct X { + int x; + void foo (int n) { + auto a1 = [=] { x = n; }; // { dg-warning "implicit capture" } + auto a2 = [=, this] { x = n; }; + auto a3 = [=, *this]() mutable { x = n; }; + auto a4 = [&] { x = n; }; + auto a5 = [&, this] { x = n; }; + auto a6 = [&, *this]() mutable { x = n; }; + + auto a7 = [=] { // { dg-warning "implicit capture" } + auto a = [=] { // { dg-warning "implicit capture" } + auto a2 = [=] { x = n; }; // { dg-warning "implicit capture" } + }; + }; + + auto a8 = [=, this] { + auto a = [=, this] { + auto a2 = [=, this] { x = n; }; + }; + }; + + auto a9 = [=, *this]() mutable { + auto a = [=, *this]() mutable { + auto a2 = [=, *this]() mutable { x = n; }; + }; + }; + + auto a10 = [&] { + auto a = [&] { + auto a2 = [&] { x = n; }; + }; + }; + + auto a11 = [&, this] { + auto a = [&, this] { + auto a2 = [&, this] { x = n; }; + }; + }; + + auto a12 = [&, *this]() mutable { + auto a = [&, *this]() mutable { + auto a2 = [&, *this]() mutable { x = n; }; + }; + }; + } +}; --- gcc/testsuite/g++.dg/cpp2a/lambda-this2.C +++ gcc/testsuite/g++.dg/cpp2a/lambda-this2.C @@ -0,0 +1,51 @@ +// P0806R2 +// { dg-do compile } +// { dg-options "-std=c++2a -Wno-deprecated" } + +struct X { + int x; + void foo (int n) { + auto a1 = [=] { x = n; }; // { dg-bogus "implicit capture" } + auto a2 = [=, this] { x = n; }; + auto a3 = [=, *this]() mutable { x = n; }; + auto a4 = [&] { x = n; }; + auto a5 = [&, this] { x = n; }; + auto a6 = [&, *this]() mutable { x = n; }; + + auto a7 = [=] { // { dg-bogus "implicit capture" } + auto a = [=] { // { dg-bogus "implicit capture" } + auto a2 = [=] { x = n; }; // { dg-bogus "implicit capture" } + }; + }; + + auto a8 = [=, this] { + auto a = [=, this] { + auto a2 = [=, this] { x = n; }; + }; + }; + + auto a9 = [=, *this]() mutable { + auto a = [=, *this]() mutable { + auto a2 = [=, *this]() mutable { x = n; }; + }; + }; + + auto a10 = [&] { + auto a = [&] { + auto a2 = [&] { x = n; }; + }; + }; + + auto a11 = [&, this] { + auto a = [&, this] { + auto a2 = [&, this] { x = n; }; + }; + }; + + auto a12 = [&, *this]() mutable { + auto a = [&, *this]() mutable { + auto a2 = [&, *this]() mutable { x = n; }; + }; + }; + } +}; --- gcc/testsuite/g++.dg/cpp2a/lambda-this3.C +++ gcc/testsuite/g++.dg/cpp2a/lambda-this3.C @@ -0,0 +1,55 @@ +// P0806R2 +// { dg-do compile } +// { dg-options "-std=c++17" } + +struct X { + int x; + void foo (int n) { + auto a1 = [=] { x = n; }; // { dg-bogus "implicit capture" } + auto a2 = [=, this] { x = n; }; + // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 } + auto a3 = [=, *this]() mutable { x = n; }; + auto a4 = [&] { x = n; }; + auto a5 = [&, this] { x = n; }; + auto a6 = [&, *this]() mutable { x = n; }; + + auto a7 = [=] { // { dg-bogus "implicit capture" } + auto a = [=] { // { dg-bogus "implicit capture" } + auto a2 = [=] { x = n; }; // { dg-bogus "implicit capture" } + }; + }; + + auto a8 = [=, this] { + // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 } + auto a = [=, this] { + // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 } + auto a2 = [=, this] { x = n; }; + // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 } + }; + }; + + auto a9 = [=, *this]() mutable { + auto a = [=, *this]() mutable { + auto a2 = [=, *this]() mutable { x = n; }; + }; + }; + + auto a10 = [&] { + auto a = [&] { + auto a2 = [&] { x = n; }; + }; + }; + + auto a11 = [&, this] { + auto a = [&, this] { + auto a2 = [&, this] { x = n; }; + }; + }; + + auto a12 = [&, *this]() mutable { + auto a = [&, *this]() mutable { + auto a2 = [&, *this]() mutable { x = n; }; + }; + }; + } +};