From patchwork Wed Feb 13 14:20:26 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 220145 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]) by ozlabs.org (Postfix) with SMTP id 30CDA2C008C for ; Thu, 14 Feb 2013 01:21:09 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1361370070; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=0n4V5Cn +CEbD6nVQoOSdY3OvVZA=; b=GIG/facXmG2iPX7HbDj2IKM/TrMF4SFJdthpsgG 7xfUxhUUc2YCP3HXpqqm7zPOOFTKmOpakHAR6mr/3BVyt3SJNsapIdwidZnkJnFl gzuRzQt3gcwfQXulxf6/xHt13ryaslFWhsm/XoNGwo3FB30uTNm5zSYHc7Lhi7PP Eta4= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Cm9EaO/lRfYFQ0f+r/GppYs0hLyaF7DfpTK3up5CVE92q2NUWKU8WVYvfhdnFc HzOE3YC03ISPAnOcg388OwXaac9lVigOtjSr3B1k0r5HdH/5DnT2BFXzfmBaOYBJ nxHtEoOsNJXfa8V+3A60Yff4Nnf6/1pE/bXV+hbMd09BA=; 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 09:20:26 -0500 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) 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 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. 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; +}