From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11269 invoked by alias); 13 Feb 2013 14:21:05 -0000 Received: (qmail 11256 invoked by uid 22791); 13 Feb 2013 14:21:04 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_SPAMHAUS_DROP,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 13 Feb 2013 14:20:28 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r1DEKRHj009425 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Feb 2013 09:20:28 -0500 Received: from [10.3.113.24] (ovpn-113-24.phx2.redhat.com [10.3.113.24]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r1DEKQFC022990 for ; Wed, 13 Feb 2013 09:20:27 -0500 Message-ID: <511BA12A.6010407@redhat.com> Date: Wed, 13 Feb 2013 14:21:00 -0000 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Thunderbird/21.0a1 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/56135 (wrong 'this' capture) Content-Type: multipart/mixed; boundary="------------080607010509090104080104" Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2013-02/txt/msg00607.txt.bz2 This is a multi-part message in MIME format. --------------080607010509090104080104 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-length: 265 When the only use of 'this' is implicitly by a dependent name, we don't see the capture until instantiation time. Don't clobber its capture list entry when we instantiate the ones seen at template definition time. Tested x86_64-pc-linux-gnu, applying to trunk. --------------080607010509090104080104 Content-Type: text/x-patch; name="56135.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="56135.patch" Content-length: 1944 commit cdcb44a750af3921d6883832892dd8cd5e2d22b4 Author: Jason Merrill Date: Tue Feb 12 21:47:56 2013 -0500 PR c++/56135 * pt.c (tsubst_copy_and_build): Don't forget any new captures that arose from use of dependent names. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index a3359ad..2aadd4d 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -14457,9 +14457,11 @@ tsubst_copy_and_build (tree t, complete_type (type); /* The capture list refers to closure members, so this needs to - wait until after we finish instantiating the type. */ + wait until after we finish instantiating the type. Also keep + any captures that may have been added during instantiation. */ LAMBDA_EXPR_CAPTURE_LIST (r) - = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)); + = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)), + LAMBDA_EXPR_CAPTURE_LIST (r)); LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE; RETURN (build_lambda_object (r)); diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C new file mode 100644 index 0000000..9309a44 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C @@ -0,0 +1,39 @@ +// PR c++/56135 +// { dg-do run { target c++11 } } + +#include + +extern "C" void abort() throw(); + +struct test { + template + std::function broken(int x) { + return [=] { +x; print(); }; + } + + std::function works0() { + return [=] { print(); }; + } + + template + std::function works1() { + return [=] { print(); }; + } + + template + std::function works2() { + return [=] { this->print(); }; + } + + template + void print() { if (this == NULL) abort (); } +}; + +int main(void) { + test().broken(1)(); + test().works0()(); + test().works1()(); + test().works2()(); + + return 0; +} --------------080607010509090104080104--