From patchwork Thu Nov 9 19:58:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1862240 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LHNqC+3j; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SRCT139cHz1yQK for ; Fri, 10 Nov 2023 06:59:00 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C4C0E383CA87 for ; Thu, 9 Nov 2023 19:58:57 +0000 (GMT) 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 [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E771D3858C54 for ; Thu, 9 Nov 2023 19:58:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E771D3858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E771D3858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699559927; cv=none; b=QncF64IXqsFRS7vzKcWI6WfWWXbo2a/3bLft9R7irYNNhOEWJLoOIWPrE2bD3KDj5/2lVM1sQ86fB8mnum6yyR/NUmTfVYDdOMfhHZPy5p2y9OBERqattr30vnQ3uUJsfcIhPG6bTDV7WdmKWwx5N6Qze7liFAx20m9voZ8RMDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699559927; c=relaxed/simple; bh=0i36eVXXI7QiiJL4xIhvSyEzNT59whbPPD+/5uCC1Us=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Dn2SIYx9Svv0RXsC4rxqIkDutEn3lg0lrQ9KnOWLjrItOp52A+/uVi/yIo8exe2FgVQ+sx8G1wYzeAFqERcCr8WGMat0SBQPejL3PFc7DnssLWiK25PwpgLd66FkM482V3175ZdNL6B70rjBPcXC6Ya1cXcKtRErMsgMhjHAe1c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699559925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=USPhW4ZiOQxS2fIMcmZxvODLOeV4PljFgzCtqr57t7Q=; b=LHNqC+3jTCs20x/klMYNjTC+7FYBtq3mOK1RNyP8kFINo/aefSw7eTkwUb1C469MM7fwty xT6sfLs56xkvis6Hd+64P5GX1vSyRaeuCodcOk3P/ZYNZ3xdgfIPLcwhvppyvZu9O5lOms rPLble/wMkTmlVJcxHR8T/zAGD74tJI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-Oia3RNm0ONW3xwzFU9B4WQ-1; Thu, 09 Nov 2023 14:58:43 -0500 X-MC-Unique: Oia3RNm0ONW3xwzFU9B4WQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 83BC229AA3B1 for ; Thu, 9 Nov 2023 19:58:43 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.33.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5FA402166B26; Thu, 9 Nov 2023 19:58:43 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: fix parsing with auto(x) [PR112410] Date: Thu, 9 Nov 2023 14:58:35 -0500 Message-ID: <20231109195835.429291-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we are wrongly parsing int y(auto(42)); which uses the C++23 cast-to-prvalue feature, and initializes y to 42. However, we were treating the auto as an implicit template parameter. Fixing the auto{42} case is easy, but when auto is followed by a (, I found the fix to be much more involved. For instance, we cannot use cp_parser_expression, because that can give hard errors. It's also necessary to disambiguate 'auto(i)' as 'auto i', not a cast. auto(), auto(int), auto(f)(int), auto(*), auto(i[]), auto(...), etc. are all function declarations. We have to look at more than one token to decide. In this fix, I'm (ab)using cp_parser_declarator, with member_p=false so that it doesn't commit. But it handles even more complicated cases as int fn (auto (*const **&f)(int) -> char); PR c++/112410 gcc/cp/ChangeLog: * parser.cc (cp_parser_simple_type_specifier): Disambiguate between a variable and function declaration with auto. (cp_parser_constructor_declarator_p): Use cp_parser_starts_param_decl_p. (cp_parser_starts_param_decl_p): New, factored out of cp_parser_constructor_declarator_p. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast13.C: New test. --- gcc/cp/parser.cc | 79 ++++++++++++++++++---- gcc/testsuite/g++.dg/cpp23/auto-fncast13.C | 61 +++++++++++++++++ 2 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast13.C base-commit: 016b3002e13acefb5773da78659c050187d3a96f diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 5116bcb78f6..3edee092e56 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -2887,6 +2887,8 @@ static bool cp_parser_next_token_ends_template_argument_p (cp_parser *); static bool cp_parser_nth_token_starts_template_argument_list_p (cp_parser *, size_t); +static bool cp_parser_starts_param_decl_p + (cp_parser *); static enum tag_types cp_parser_token_is_class_key (cp_token *); static enum tag_types cp_parser_token_is_type_parameter_key @@ -19991,6 +19993,8 @@ cp_parser_simple_type_specifier (cp_parser* parser, /* The 'auto' might be the placeholder return type for a function decl with trailing return type. */ bool have_trailing_return_fn_decl = false; + /* Or it might be auto(x) or auto {x}. */ + bool decay_copy = false; cp_parser_parse_tentatively (parser); cp_lexer_consume_token (parser->lexer); @@ -20002,12 +20006,43 @@ cp_parser_simple_type_specifier (cp_parser* parser, if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) { cp_lexer_consume_token (parser->lexer); + /* An auto specifier that appears in a parameter declaration + might be the placeholder for a late return type, or it + can be an implicit template parameter. But it can also + be a prvalue cast, rendering the current construct not + a function declaration at all. Check if it decidedly + cannot be a valid function-style cast first... */ + if (!cp_parser_starts_param_decl_p (parser)) + { + /* Ug, we couldn't tell. Try to parse whatever follows + as a declarator; this should detect cases like + auto(i), auto(*), auto(f[]), auto(f)(int). */ + cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, + CP_PARSER_FLAGS_NONE, + /*ctor_dtor_or_conv_p=*/nullptr, + /*parenthesized_p=*/NULL, + /*member_p=*/true, + /*friend_p=*/false, + /*static_p=*/true); + /* OK, if we now see a ')', it looks like a valid + function declaration. Otherwise, let's go with + auto(x). */ + decay_copy + = cp_lexer_next_token_is_not (parser->lexer, + CPP_CLOSE_PAREN); + } cp_parser_skip_to_closing_parenthesis (parser, /*recovering*/false, /*or_comma*/false, /*consume_paren*/true); continue; } + /* The easy case: it has got to be C++23 auto(x). */ + else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) + { + decay_copy = true; + break; + } if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF)) { @@ -20019,6 +20054,12 @@ cp_parser_simple_type_specifier (cp_parser* parser, } cp_parser_abort_tentative_parse (parser); + if (decay_copy) + { + type = error_mark_node; + break; + } + if (have_trailing_return_fn_decl) { type = make_auto (); @@ -32066,21 +32107,7 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) constructor_p = false; - if (constructor_p - && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) - && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) - /* A parameter declaration begins with a decl-specifier, - which is either the "attribute" keyword, a storage class - specifier, or (usually) a type-specifier. */ - && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer) - /* GNU attributes can actually appear both at the start of - a parameter and parenthesized declarator. - S (__attribute__((unused)) int); - is a constructor, but - S (__attribute__((unused)) foo) (int); - is a function declaration. [[attribute]] can appear in the - first form too, but not in the second form. */ - && !cp_next_tokens_can_be_std_attribute_p (parser)) + if (constructor_p && !cp_parser_starts_param_decl_p (parser)) { tree type; tree pushed_scope = NULL_TREE; @@ -34334,6 +34361,28 @@ cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, return false; } +/* We've consumed a '(' and now we're asking if what follows starts + a parameter declaration. Return true if it does. */ + +static bool +cp_parser_starts_param_decl_p (cp_parser *parser) +{ + return (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) + || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) + /* A parameter declaration begins with a decl-specifier, + which is either the "attribute" keyword, a storage class + specifier, or (usually) a type-specifier. */ + || cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer) + /* GNU attributes can actually appear both at the start of + a parameter and parenthesized declarator. + S (__attribute__((unused)) int); + is a constructor, but + S (__attribute__((unused)) foo) (int); + is a function declaration. [[attribute]] can appear in the + first form too, but not in the second form. */ + || cp_next_tokens_can_be_std_attribute_p (parser)); +} + /* Returns the kind of tag indicated by TOKEN, if it is a class-key, or none_type otherwise. */ diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast13.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast13.C new file mode 100644 index 00000000000..1bceffb70cf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast13.C @@ -0,0 +1,61 @@ +// PR c++/112410 +// { dg-do compile { target c++23 } } + +int f1 (auto(int) -> char); +int f2 (auto x); +int f3 (auto); +int f4 (auto(i)); + +int v1 (auto(42)); +int v2 (auto{42}); +int e1 (auto{i}); // { dg-error "not declared" } +int i; +int v3 (auto{i}); +int v4 (auto(i + 1)); +int v5 (auto(+i)); +int v6 (auto(i = 4)); + +int f5 (auto(i)); +int f6 (auto()); +int f7 (auto(int)); +int f8 (auto(f)(int)); +int f9 (auto(...) -> char); +// FIXME: ICEs (PR c++/89867) +//int f10 (auto(__attribute__((unused)) i)); +int f11 (auto((i))); +int f12 (auto(i[])); +int f13 (auto(*i)); +int f14 (auto(*)); + +int e2 (auto{}); // { dg-error "invalid use of .auto." } +int e3 (auto(i, i)); // { dg-error "invalid use of .auto." } + +char bar (int); +char baz (); +char qux (...); + +void +g (int i) +{ + f1 (bar); + f2 (42); + f3 (42); + f4 (42); + f5 (42); + f6 (baz); + f7 (bar); + f8 (bar); + f9 (qux); +// f10 (42); + f11 (42); + f12 (&i); + f13 (&i); + f14 (&i); + + v1 = 1; + v2 = 2; + v3 = 3; + v4 = 4; + v5 = 5; + v6 = 6; +}