From patchwork Thu Oct 21 22:27:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1544709 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=kvNdQEuF; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Hb2Dl38jhz9sPB for ; Fri, 22 Oct 2021 09:28:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D460D3857818 for ; Thu, 21 Oct 2021 22:28:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D460D3857818 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634855284; bh=KwKjy4NfKieiTqVAPGt7dIBIG1s8eOfoLH9DOQKlEag=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=kvNdQEuFK2RPyhTRxp0O4p2ksrnAKeoy3v1eSPyuQsmmPtsYjTAXdbUBT4TyxDA/r TWBTLUU3RJiwK67MdiLh1IZ7f1uNwIBqBRMnTRrElJm7+GQcMhDLSCIhR7ZViXiDCp 6lVVmmXb4h5hea42D/tBMtRz8M8d5xj8VmvznssA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 154FA3858416 for ; Thu, 21 Oct 2021 22:27:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 154FA3858416 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-547-Pp62utZZNkiApe_gqwWlog-1; Thu, 21 Oct 2021 18:27:21 -0400 X-MC-Unique: Pp62utZZNkiApe_gqwWlog-1 Received: by mail-qt1-f200.google.com with SMTP id k1-20020ac80201000000b002a7200b449eso1457775qtg.18 for ; Thu, 21 Oct 2021 15:27:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=KwKjy4NfKieiTqVAPGt7dIBIG1s8eOfoLH9DOQKlEag=; b=uytxUQe7qOTgKvQetyRpbmkmJAfsPAyJhvJ+R0Ihjm/0LSR0e2QntA8YzKewzWAoJr 2r6DqxHmH1Xwg8xY5aJ6UgaKjkf1PtoM31W94vVnj+91E9SwrrppPjdr1m15xh2VQmvk 3zkXJzDvqVRjdozB4+/R0Z86C5WIwuTfSmLBRIfugFhfFnBBfvO9UZQT1fLwEVzWh8yy x9i4fX1y+o616cFM268AE0sorYXv6dGJcX5s2u8xuoRGE9Wz+3qIGjd+CWeRxry4Fqul S3os1Wt6Pme1y5xMcrWKV8DEMk4WqdVNYJJ87ZvGqe3k7FJRDZ2dy1Y4jRM/k70cz+Ci SBjA== X-Gm-Message-State: AOAM533z/fJ1lpb+Or0Cqu7SMw6FPuY5ptVuE7itSTkPW1EIx2CWlhJP g+iXU6G5SbN2O1MChH4R8hzaj6J7OUY644ix9pU1MSO9idoI+dlAmldtahc3xhVj38qul97D5Bp vkE6XvLsphirmzgeEiw== X-Received: by 2002:ac8:7f4b:: with SMTP id g11mr8800185qtk.339.1634855240595; Thu, 21 Oct 2021 15:27:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJUsXMXGisWmfkZckPd0e5QZ5HV7YgDdjcjqnrRp8x1MjUEG6tUzxeZcDTYNfyhEWG303isw== X-Received: by 2002:ac8:7f4b:: with SMTP id g11mr8800165qtk.339.1634855240292; Thu, 21 Oct 2021 15:27:20 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::9b82]) by smtp.gmail.com with ESMTPSA id 14sm3532659qtp.97.2021.10.21.15.27.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 15:27:19 -0700 (PDT) Date: Thu, 21 Oct 2021 18:27:18 -0400 To: Jason Merrill Subject: [PATCH v2] c++: P2360R0: Extend init-stmt to allow alias-decl [PR102617] Message-ID: References: <20211021202610.298481-1-polacek@redhat.com> <3485dcae-73c0-c68e-dc64-9d2a7ed774ae@redhat.com> MIME-Version: 1.0 In-Reply-To: <3485dcae-73c0-c68e-dc64-9d2a7ed774ae@redhat.com> User-Agent: Mutt/2.0.7 (2021-05-04) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" On Thu, Oct 21, 2021 at 04:56:57PM -0400, Jason Merrill wrote: > On 10/21/21 16:26, Marek Polacek wrote: > > The following patch implements C++23 P2360R0. This proposal merely > > extends init-statement to contain alias-declaration. init-statement > > is used in if/for/switch. The unsightly duplication of the new code > > seems to be necessary to handle > > > > for ( init-statement condition[opt] ; expression[opt] ) statement > > > > as well as > > > > for ( init-statement[opt] for-range-declaration : for-range-initializer ) statement > > It seems like the duplication of the new code is a consequence of the > duplication of the old code. I'd think we could remove the duplication by > remembering the result of cp_parser_range_based_for_with_init_p and then > recursing at the end if it was true. Or check it in cp_parser_for and call > cp_parser_init_statement twice. That works well, just had to move the pedwarn too. dg.exp passes, full testing running, OK if it passes? -- >8 -- The following patch implements C++23 P2360R0. This proposal merely extends init-statement to contain alias-declaration. init-statement is used in if/for/switch. It also removes the unsightly duplication of code by calling cp_parser_init_statement twice. PR c++/102617 gcc/cp/ChangeLog: * parser.c (cp_parser_for): Maybe call cp_parser_init_statement twice. Warn about range-based for loops with initializer here. (cp_parser_init_statement): Don't duplicate code. Allow alias-declaration in init-statement. gcc/testsuite/ChangeLog: * g++.dg/cpp23/init-stmt1.C: New test. * g++.dg/cpp23/init-stmt2.C: New test. --- gcc/cp/parser.c | 70 ++++++++++++++----------- gcc/testsuite/g++.dg/cpp23/init-stmt1.C | 31 +++++++++++ gcc/testsuite/g++.dg/cpp23/init-stmt2.C | 25 +++++++++ 3 files changed, 95 insertions(+), 31 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/init-stmt1.C create mode 100644 gcc/testsuite/g++.dg/cpp23/init-stmt2.C base-commit: 1373066a46d8d47abd97e46a005aef3b3dbfe94a diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 49d951cfb19..93335c817d7 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -12040,6 +12040,7 @@ cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs, init-statement: expression-statement simple-declaration + alias-declaration TM Extension: @@ -13327,6 +13328,23 @@ cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll) /* Begin the for-statement. */ scope = begin_for_scope (&init); + /* Maybe parse the optional init-statement in a range-based for loop. */ + if (cp_parser_range_based_for_with_init_p (parser) + /* Checked for diagnostic purposes only. */ + && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) + { + tree dummy; + cp_parser_init_statement (parser, &dummy); + if (cxx_dialect < cxx20) + { + pedwarn (cp_lexer_peek_token (parser->lexer)->location, + OPT_Wc__20_extensions, + "range-based % loops with initializer only " + "available with %<-std=c++20%> or %<-std=gnu++20%>"); + decl = error_mark_node; + } + } + /* Parse the initialization. */ is_range_for = cp_parser_init_statement (parser, &decl); @@ -13987,12 +14005,13 @@ cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep, return statement; } -/* Parse a init-statement or the declarator of a range-based-for. +/* Parse an init-statement or the declarator of a range-based-for. Returns true if a range-based-for declaration is seen. init-statement: expression-statement - simple-declaration */ + simple-declaration + alias-declaration */ static bool cp_parser_init_statement (cp_parser *parser, tree *decl) @@ -14008,40 +14027,29 @@ cp_parser_init_statement (cp_parser *parser, tree *decl) bool is_range_for = false; bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; - /* Try to parse the init-statement. */ - if (cp_parser_range_based_for_with_init_p (parser)) - { - tree dummy; - cp_parser_parse_tentatively (parser); - /* Parse the declaration. */ - cp_parser_simple_declaration (parser, - /*function_definition_allowed_p=*/false, - &dummy); - cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); - if (!cp_parser_parse_definitely (parser)) - /* That didn't work, try to parse it as an expression-statement. */ - cp_parser_expression_statement (parser, NULL_TREE); - - if (cxx_dialect < cxx20) - { - pedwarn (cp_lexer_peek_token (parser->lexer)->location, - OPT_Wc__20_extensions, - "range-based % loops with initializer only " - "available with %<-std=c++20%> or %<-std=gnu++20%>"); - *decl = error_mark_node; - } - } - /* A colon is used in range-based for. */ parser->colon_corrects_to_scope_p = false; /* We're going to speculatively look for a declaration, falling back to an expression, if necessary. */ cp_parser_parse_tentatively (parser); - /* Parse the declaration. */ - cp_parser_simple_declaration (parser, - /*function_definition_allowed_p=*/false, - decl); + bool expect_semicolon_p = true; + if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) + { + cp_parser_alias_declaration (parser); + expect_semicolon_p = false; + if (cxx_dialect < cxx23 + && !cp_parser_uncommitted_to_tentative_parse_p (parser)) + pedwarn (cp_lexer_peek_token (parser->lexer)->location, + OPT_Wc__23_extensions, + "alias-declaration in init-statement only " + "available with %<-std=c++23%> or %<-std=gnu++23%>"); + } + else + /* Parse the declaration. */ + cp_parser_simple_declaration (parser, + /*function_definition_allowed_p=*/false, + decl); parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) { @@ -14054,7 +14062,7 @@ cp_parser_init_statement (cp_parser *parser, tree *decl) "range-based % loops only available with " "%<-std=c++11%> or %<-std=gnu++11%>"); } - else + else if (expect_semicolon_p) /* The ';' is not consumed yet because we told cp_parser_simple_declaration not to. */ cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); diff --git a/gcc/testsuite/g++.dg/cpp23/init-stmt1.C b/gcc/testsuite/g++.dg/cpp23/init-stmt1.C new file mode 100644 index 00000000000..29e3256aae6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/init-stmt1.C @@ -0,0 +1,31 @@ +// PR c++/102617 +// P2360R0: Extend init-statement to allow alias-declaration +// { dg-do compile { target c++20 } } +// Test valid use. + +int v[10]; + +void +g () +{ + for (using T = int; (T) false;) // { dg-error "only available with" "" { target c++20_only } } + ; + for (using T = int; T e : v) // { dg-error "only available with" "" { target c++20_only } } + (void) e; + if (using T = int; true) // { dg-error "only available with" "" { target c++20_only } } + { + T x = 0; + (void) x; + } + if constexpr (using T = int; true) // { dg-error "only available with" "" { target c++20_only } } + { + T x = 0; + (void) x; + } + switch (using T = int; 42) // { dg-error "only available with" "" { target c++20_only } } + case 42: + { + T x = 0; + (void) x; + } +} diff --git a/gcc/testsuite/g++.dg/cpp23/init-stmt2.C b/gcc/testsuite/g++.dg/cpp23/init-stmt2.C new file mode 100644 index 00000000000..ca6201bc340 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/init-stmt2.C @@ -0,0 +1,25 @@ +// PR c++/102617 +// P2360R0: Extend init-statement to allow alias-declaration +// { dg-do compile { target c++23 } } +// Test invalid use. + +int v[10]; +namespace N { using X = int; } + +void +g () +{ + for (using N::X; false;) // { dg-error "expected" } + ; + for (using N::X; int e : v) // { dg-error "expected" } + (void) e; + for (using T = int; using U = int; int e : v) // { dg-error "" } + ; + if (using N::X; false) // { dg-error "expected" } + {} + switch (using N::X; 0) // { dg-error "expected" } + ; + if (using T = int;) // { dg-error "expected" } + { + } +}