From patchwork Tue Jul 9 17:43:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 257849 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id E54A02C00A0 for ; Wed, 10 Jul 2013 03:44:12 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; q= dns; s=default; b=fHHTWpuVewrRt8qa7dSaOmVYZBNg2b8Y7k2NR1/keqEZMw +Fnl/kEH4wwITDYVheZ1LNfYCW2fHXtEWwQqoBSSZvUpZx9ZTYDWSY82bW5yirOo id0LKk7n/NhP9K9dECPwImaED3acug6DlI5E/sS1BYU6AL7KbdTj9H98Fc004= 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 :message-id:date:from:mime-version:to:subject:content-type; s= default; bh=jMM5SByWU76Fz417O5EsuFSXJoI=; b=ckkZRMgI3PY8HBdz39K+ Hyst16voS3f64mQRBUJCSn1mmRdn65NVkHC5qaCT26r6GJd35M1m+H3nbKY2xS0z Sx9D5kmkR+sKPjRyD2VHJVY+HzJOOc4AFUxyGaKw2fvMCRQy92bqougQU5BGuwjK Iey7xgb/clNGXF9t+Gs/C3o= Received: (qmail 12996 invoked by alias); 9 Jul 2013 17:44:06 -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 12986 invoked by uid 89); 9 Jul 2013 17:44:06 -0000 X-Spam-SWARE-Status: No, score=-6.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS, TW_NR autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Tue, 09 Jul 2013 17:44:05 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r69Hi4Ax011886 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 9 Jul 2013 13:44:04 -0400 Received: from [10.3.113.19] ([10.3.113.19]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r69Hi1fY023215 for ; Tue, 9 Jul 2013 13:44:03 -0400 Message-ID: <51DC4BDF.9070706@redhat.com> Date: Tue, 09 Jul 2013 13:43:59 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0a2 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/57437 (wrong code with mutable lambda) X-Virus-Found: No Here, we were wrongly treating a lambda capture proxy as a local variable for the purpose of C++11 return by move. Fixed by preventing it for proxy variables of all kinds. Tested x86_64-pc-linux-gnu, applying to trunk, 4.8, 4.7. commit a7dc31796b5a7aab2818fa9cb7e0f345ecc8fdf0 Author: Jason Merrill Date: Tue Jul 9 02:51:34 2013 -0400 PR c++/57437 * typeck.c (check_return_expr): Lambda proxies aren't eligible for nrv or return by move. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 462abdd..6f7d489 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -8399,7 +8399,8 @@ check_return_expr (tree retval, bool *no_warning) && VAR_P (retval) && DECL_CONTEXT (retval) == current_function_decl && ! TREE_STATIC (retval) - && ! DECL_ANON_UNION_VAR_P (retval) + /* And not a lambda or anonymous union proxy. */ + && !DECL_HAS_VALUE_EXPR_P (retval) && (DECL_ALIGN (retval) <= DECL_ALIGN (result)) /* The cv-unqualified type of the returned value must be the same as the cv-unqualified return type of the @@ -8444,7 +8445,7 @@ check_return_expr (tree retval, bool *no_warning) Note that these conditions are similar to, but not as strict as, the conditions for the named return value optimization. */ if ((cxx_dialect != cxx98) - && (VAR_P (retval) + && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval)) || TREE_CODE (retval) == PARM_DECL) && DECL_CONTEXT (retval) == current_function_decl && !TREE_STATIC (retval) diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-return1.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-return1.C new file mode 100644 index 0000000..4b353b6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-return1.C @@ -0,0 +1,26 @@ +// PR c++/57437 +// { dg-require-effective-target c++11 } + +struct A { + int i; + + A(): i(42) {} + A(const A&) = default; + A(A&& a): i(a.i) { a.i = 0; } +}; + +int main() +{ + A x; + + auto y = [x] () mutable { + x.i++; + return x; + }; + + if (y().i != 43) + __builtin_abort (); + + if (y().i != 44) + __builtin_abort (); +}