From patchwork Fri Feb 15 19:21:57 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 220829 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 571E72C0082 for ; Sat, 16 Feb 2013 06:22:24 +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=1361560945; 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=zIx+3Mx lXkbBhkacuoHe3TQEuzk=; b=YvAjXDukEI2W0lOZH3koalcTs41n0/DiHGrTVtm wUvLjOBF9RLdcVmmQpEFqKLfPTJK4LiPX/pzjsEDqxsGhD9Zgmmx6x/+35TGG2zM 8w83zdqZiCvs6TDVPoj826Y7dx1BRwcl3S9E7S8dQJ5yyt47PnO1TbmYyEDilJnT ZNfY= 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=AfOO9TjD1+hqp1xSxCsBI2IdbGU6AupmdvfVIVjyNPqMEd/JfByxrr7F9RUbCw 5GKwkP/Ec6/9etEQIHNl1XJUNM4v6+4TQcsOkMUc1DMZT7ZH5vGgo9uFfvOoW9Vg RnKRZ+oq9r5cFrIZeTMnUvYbUzlAci8l0hYaJGOWj9l0I=; Received: (qmail 28268 invoked by alias); 15 Feb 2013 19:22:15 -0000 Received: (qmail 28187 invoked by uid 22791); 15 Feb 2013 19:22:15 -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; Fri, 15 Feb 2013 19:21:59 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r1FJLwm9003836 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 15 Feb 2013 14:21:59 -0500 Received: from [10.3.113.52] (ovpn-113-52.phx2.redhat.com [10.3.113.52]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r1FJLwiL019844 for ; Fri, 15 Feb 2013 14:21:58 -0500 Message-ID: <511E8AD5.8080405@redhat.com> Date: Fri, 15 Feb 2013 14:21:57 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20100101 Thunderbird/20.0a2 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/54276 (link error with lambda and static var) 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 Like 52026, the problem here is that we have a reference to an outer variable in the lambda and can't instantiate it properly. And like 52026, the solution is to look up the variable again at instantiation time. Tested x86_64-pc-linux-gnu, applying to trunk. commit 43d4443096a0dbb9e9424370f010372ef79717e8 Author: Jason Merrill Date: Fri Feb 15 13:52:14 2013 -0500 PR c++/54276 * semantics.c (finish_id_expression): Also return the identifier for an outer local static. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 0e09d04..458ed26 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2876,18 +2876,26 @@ baselink_for_fns (tree fns) return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE); } -/* Returns true iff DECL is an automatic variable from a function outside +/* Returns true iff DECL is a variable from a function outside the current one. */ static bool -outer_automatic_var_p (tree decl) +outer_var_p (tree decl) { return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) && DECL_FUNCTION_SCOPE_P (decl) - && !TREE_STATIC (decl) && DECL_CONTEXT (decl) != current_function_decl); } +/* As above, but also checks that DECL is automatic. */ + +static bool +outer_automatic_var_p (tree decl) +{ + return (outer_var_p (decl) + && !TREE_STATIC (decl)); +} + /* ID_EXPRESSION is a representation of parsed, but unprocessed, id-expression. (See cp_parser_id_expression for details.) SCOPE, if non-NULL, is the type or namespace used to explicitly qualify @@ -2994,9 +3002,18 @@ finish_id_expression (tree id_expression, /* Disallow uses of local variables from containing functions, except within lambda-expressions. */ - if (outer_automatic_var_p (decl) + if (!outer_var_p (decl) /* It's not a use (3.2) if we're in an unevaluated context. */ - && !cp_unevaluated_operand) + || cp_unevaluated_operand) + /* OK. */; + else if (TREE_STATIC (decl)) + { + if (processing_template_decl) + /* For a use of an outer static var, return the identifier so + that we'll look it up again in the instantiation. */ + return id_expression; + } + else { tree context = DECL_CONTEXT (decl); tree containing_function = current_function_decl; diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-template9.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-template9.C new file mode 100644 index 0000000..c1d010b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-template9.C @@ -0,0 +1,15 @@ +// PR c++/54276 +// { dg-do link { target c++11 } } + +template +void foo(T) +{ + static int x = 1; + auto f = [] { return x + 1; }; + f(); +} + +int main() +{ + foo(4); +}