From patchwork Mon Oct 17 18:58:00 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 120285 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 A6E50B70B7 for ; Tue, 18 Oct 2011 05:58:22 +1100 (EST) Received: (qmail 9697 invoked by alias); 17 Oct 2011 18:58:21 -0000 Received: (qmail 9680 invoked by uid 22791); 17 Oct 2011 18:58:20 -0000 X-SWARE-Spam-Status: No, hits=-6.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, 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; Mon, 17 Oct 2011 18:58:03 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p9HIw37R001981 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 17 Oct 2011 14:58:03 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p9HIw2Ev005731 for ; Mon, 17 Oct 2011 14:58:02 -0400 Received: from [0.0.0.0] (ovpn-113-56.phx2.redhat.com [10.3.113.56]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p9HIw1no010247 for ; Mon, 17 Oct 2011 14:58:01 -0400 Message-ID: <4E9C7AB8.9070600@redhat.com> Date: Mon, 17 Oct 2011 14:58:00 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20111001 Thunderbird/7.0.1 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/50736 (accepting invalid lambda captures) 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 Here we were failing to check whether the thing being captured was an automatic variable, as required by the standard. Tested x86_64-pc-linux-gnu, applying to trunk. commit 2e585b32e7c969c1dc2ae7b2a62978b95ed4bf17 Author: Jason Merrill Date: Sun Oct 16 01:15:09 2011 -0400 PR c++/50736 * parser.c (cp_parser_lambda_introducer): Check for more invalid captures. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index ea0c4dc..bf362f2 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -7630,6 +7630,31 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) /*ambiguous_decls=*/NULL, capture_token->location); + if (capture_init_expr == error_mark_node) + { + unqualified_name_lookup_error (capture_id); + continue; + } + else if (DECL_P (capture_init_expr) + && (TREE_CODE (capture_init_expr) != VAR_DECL + && TREE_CODE (capture_init_expr) != PARM_DECL)) + { + error_at (capture_token->location, + "capture of non-variable %qD ", + capture_init_expr); + inform (0, "%q+#D declared here", capture_init_expr); + continue; + } + if (TREE_CODE (capture_init_expr) == VAR_DECL + && decl_storage_duration (capture_init_expr) != dk_auto) + { + pedwarn (capture_token->location, 0, "capture of variable " + "%qD with non-automatic storage duration", + capture_init_expr); + inform (0, "%q+#D declared here", capture_init_expr); + continue; + } + capture_init_expr = finish_id_expression (capture_id, @@ -7647,10 +7672,6 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) capture_token->location); } - if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE) - capture_init_expr - = unqualified_name_lookup_error (capture_init_expr); - if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE && !explicit_init_p) { diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-capture-neg.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-capture-neg.C new file mode 100644 index 0000000..82cc984 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-capture-neg.C @@ -0,0 +1,15 @@ +// PR c++/50736 +// { dg-options "-std=c++0x -pedantic-errors" } + +int i; +void f(); +typedef int T; + +int main() +{ + [i]{}; // { dg-error "non-automatic" } + [f]{}; // { dg-error "non-variable" } + [T]{}; // { dg-error "non-variable" } +} + +struct A { };