From patchwork Thu Jun 14 13:00:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 929428 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-479717-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="mk19GvoR"; 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 4163dp61DQz9ryk for ; Thu, 14 Jun 2018 23:01:01 +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:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=okAW5sWwZ/meOpjLYEWYOugtuD7LX OFG+RbWLdyx5Ys8fj17ZfnwV7ak3IfjdubA2ZGMbe0mcD3KGwaNkGC0hrwixxu8P q9VOvbQI5KcyJAJwuLz0XJUeu1gw936HapY+3ob1BUpZMNLMpX6wjkpQT7OgkZub 94Qujg3fRnPJkw= 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:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=zaOO8WTxQCevzfenpHx6WGL89+Q=; b=mk1 9GvoR7vZhSpXykqAeYadtPxBYYBbJMx3DA9ntuIgcwS8r0cFH9lmmhBphCU3VPfS qdcruuKl9vlzEbxWGmicOpBZJ+I0CeNofBnwcdkkHtNu+oaq9r6pevNb0psxhMB4 8/ZwBnPzBdgC5kAWp6zzzTvWYFDu5kF+wienWmcc= Received: (qmail 114257 invoked by alias); 14 Jun 2018 13:00:54 -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 114041 invoked by uid 89); 14 Jun 2018 13:00:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=gt 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; Thu, 14 Jun 2018 13:00:27 +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 AE329307D858 for ; Thu, 14 Jun 2018 13:00:09 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-117-37.ams2.redhat.com [10.36.117.37]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3E46E3001721; Thu, 14 Jun 2018 13:00:09 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id w5ED072p032596; Thu, 14 Jun 2018 15:00:07 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id w5ED06tp032595; Thu, 14 Jun 2018 15:00:06 +0200 Date: Thu, 14 Jun 2018 15:00:06 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] P0624R2 - Default constructible and assignable stateless lambdas Message-ID: <20180614130006.GS7166@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.2 (2017-12-15) X-IsSubscribed: yes Hi! The following patch implements P0624R2, where stateless lambdas don't have deleted default ctor and copy assignment operator anymore. Tested on x86_64-linux with check-c++-all and libstdc++ testsuite, ok for trunk? 2018-06-14 Jakub Jelinek P0624R2 - Default constructible and assignable stateless lambdas * method.c (synthesized_method_walk): For C++2a don't mark sfk_constructor or sfk_copy_assignment as deleted if lambda has no lambda-captures. * g++.dg/cpp2a/lambda1.C: New test. * g++.dg/cpp0x/lambda/lambda-ice2.C: Adjust expected diagnostics for -std=c++2a. Jakub --- gcc/cp/method.c.jj 2018-05-21 13:15:33.519575351 +0200 +++ gcc/cp/method.c 2018-06-14 13:48:58.825585083 +0200 @@ -1539,10 +1539,15 @@ synthesized_method_walk (tree ctype, spe { /* "The closure type associated with a lambda-expression has a deleted default constructor and a deleted copy assignment operator." - This is diagnosed in maybe_explain_implicit_delete. */ + This is diagnosed in maybe_explain_implicit_delete. + In C++2a, only lambda-expressions with lambda-captures have those + deleted. */ if (LAMBDA_TYPE_P (ctype) - && (sfk == sfk_constructor - || sfk == sfk_copy_assignment)) + && (sfk == sfk_constructor || sfk == sfk_copy_assignment) + && (cxx_dialect < cxx2a + || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype)) + || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE + (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE)) { *deleted_p = true; return; --- gcc/testsuite/g++.dg/cpp2a/lambda1.C.jj 2018-06-14 14:27:29.903602374 +0200 +++ gcc/testsuite/g++.dg/cpp2a/lambda1.C 2018-06-14 14:26:51.462571226 +0200 @@ -0,0 +1,58 @@ +// P0624R2 +// { dg-do compile { target c++11 } } + +#if __cplusplus >= 201402L +#define A auto +#else +#define A int +#endif + +void +f1 () +{ + auto greater = [](A x, A y) { return x > y; }; // { dg-message "a lambda closure type has a deleted (default constructor|copy assignment operator)" "" { target c++17_down } } + decltype (greater) gt; // { dg-error "use of deleted function" "" { target c++17_down } } + gt = greater; // { dg-error "use of deleted function" "" { target c++17_down } } +} + +void +f2 () +{ + auto greater = [&](A x, A y) { return x > y; }; // { dg-message "a lambda closure type has a deleted (default constructor|copy assignment operator)" } + decltype (greater) gt; // { dg-error "use of deleted function" } + gt = greater; // { dg-error "use of deleted function" } +} + +void +f3 () +{ + auto greater = [=](A x, A y) { return x > y; }; // { dg-message "a lambda closure type has a deleted (default constructor|copy assignment operator)" } + decltype (greater) gt; // { dg-error "use of deleted function" } + gt = greater; // { dg-error "use of deleted function" } +} + +void +f4 (int i) +{ + auto greater = [i](A x, A y) { return x > y; }; // { dg-message "a lambda closure type has a deleted (default constructor|copy assignment operator)" } + decltype (greater) gt; // { dg-error "use of deleted function" } + gt = greater; // { dg-error "use of deleted function" } +} + +#if __cplusplus > 201703L +void +f5 () +{ + auto greater = [](auto x, auto y) constexpr { return x > y; }; + decltype (greater) gt; + static_assert (!gt (1, 2)); + static_assert (gt (4, 3)); + static_assert (!gt (3.5, 3.75)); + static_assert (gt (3.5, 3.25)); + gt = greater; + static_assert (!gt (1, 2)); + static_assert (gt (4, 3)); + static_assert (!gt (3.5, 3.75)); + static_assert (gt (3.5, 3.25)); +} +#endif --- gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ice2.C.jj 2014-05-15 11:58:42.000000000 +0200 +++ gcc/testsuite/g++.dg/cpp0x/lambda/lambda-ice2.C 2018-06-14 14:32:03.760824236 +0200 @@ -2,15 +2,15 @@ // { dg-do compile { target c++11 } } template -decltype(F()) run(F f) // { dg-message "note" } +decltype(F()) run(F f) // { dg-message "note" "" { target c++17_down } } { - return f(); + return f(); // { dg-error "could not convert" "" { target c++2a } } } int main() { - auto l = []() { return 5; }; // { dg-message "lambda closure type" } + auto l = []() { return 5; }; // { dg-message "lambda closure type" "" { target c++17_down } } - run(l); // { dg-error "no match" } - // { dg-error "use of deleted function" "candidate explanation" { target *-*-* } 5 } + run(l); // { dg-error "no match" "" { target c++17_down } } + // { dg-error "use of deleted function" "candidate explanation" { target c++17_down } 5 } }