From patchwork Thu Dec 14 21:02:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1876385 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=VNa8GNtC; 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 4SrlDR5Y52z23nt for ; Fri, 15 Dec 2023 08:02:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7E9438618D3 for ; Thu, 14 Dec 2023 21:02:45 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 954693858D20 for ; Thu, 14 Dec 2023 21:02:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 954693858D20 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 954693858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702587756; cv=none; b=SqPMfhi+jb5zArTZW3gxnMYJ1/nmQ5AQeqTYX2pESgqtRbEGb3G3J45PIjCrIhRZ1304E1pi9llVaoYYmnc66Pu6+iKVPchiHMAx79DnGM81GvWeZ2qJPMRz84mez21eNb0hBomB0a2xQgn6S38y59tHe4Ipmk5+QW8gG9UcZQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702587756; c=relaxed/simple; bh=DIkwx2IcHD3Pe68/IkvLzjZTtDX5luR8Mjlw2j05JyQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZvyC+Nlpbccwl015lC73zjSVVDvslxjZf7N1aqhf4L5SMzubMwYNxq/56N1Rk2/4AB4xMO9Tc0z4huKv9m2EcwKV/yBt6DFwrQpIwhEukoIGNF4iNToCR8KsI8o8NZBeooBfuAIUh23LjaIAQvTMdt57OJryjlwGldaXC1XCpIY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702587754; 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=aayKGQs3+oz2mNwInt/dSODZT0+vofd2O54Wq1YtkOQ=; b=VNa8GNtCgmzpCoIAOVRattDYbChQN/tldS4YlBPkL49aXVifr3fX/+AbXSiYwMEykEI41b P7Ye/1K7SbUHx8RiMfsdVLZelhsLpadmFY8gkvrsImDFjTVACLFSiOsmVI/96f51p8e7r9 k64hZvSk1BZ3mU2gUMTiJCP4/TP9r2M= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-104-iW2OHSOVPoimX0Xvud5IQw-1; Thu, 14 Dec 2023 16:02:33 -0500 X-MC-Unique: iW2OHSOVPoimX0Xvud5IQw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 B805583BA8A for ; Thu, 14 Dec 2023 21:02:32 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99801492BF0; Thu, 14 Dec 2023 21:02:32 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: fix parsing with auto(x) at block scope [PR112482] Date: Thu, 14 Dec 2023 16:02:27 -0500 Message-ID: <20231214210227.1190177-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_NONE, RCVD_IN_MSPIKE_H3, 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 -- This is sort of like r14-5514, but at block scope. Consider struct A { A(int, int); }; void g (int a) { A bar(auto(a), 42); // not a fn decl } where we emit error: 'auto' parameter not permitted in this context which is bogus -- bar doesn't declare a function, so the auto is OK, but we don't know it till we've seen the second argument. The error comes from grokdeclarator invoked just after we've parsed the auto(a). A possible approach seems to be to delay the auto parameter checking and only check once we know we are indeed dealing with a function declaration. For tparms, we should still emit the error right away. PR c++/112482 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Do not issue the auto parameter error while tentatively parsing a function parameter. * parser.cc (cp_parser_parameter_declaration_clause): Check it here. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast15.C: New test. --- gcc/cp/decl.cc | 13 +++++++++++-- gcc/cp/parser.cc | 17 +++++++++++++++-- gcc/testsuite/g++.dg/cpp23/auto-fncast15.C | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast15.C base-commit: 767e2674875139ac8f354ceee655c1a9561b9779 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 4d17ead123a..1ffe4c82748 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14203,6 +14203,7 @@ grokdeclarator (const cp_declarator *declarator, tree auto_node = type_uses_auto (type); if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag)) { + bool err_p = true; if (cxx_dialect >= cxx14) { if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node)) @@ -14221,13 +14222,21 @@ grokdeclarator (const cp_declarator *declarator, "abbreviated function template"); inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c); } - else + else if (decl_context == CATCHPARM || template_parm_flag) error_at (typespec_loc, "% parameter not permitted in this context"); + else + /* Do not issue an error while tentatively parsing a function + parameter: for T t(auto(a), 42);, when we just saw the 1st + parameter, we don't know yet that this construct won't be + a function declaration. Defer the checking to + cp_parser_parameter_declaration_clause. */ + err_p = false; } else error_at (typespec_loc, "parameter declared %"); - type = error_mark_node; + if (err_p) + type = error_mark_node; } /* A parameter declared as an array of T is really a pointer to T. diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 58e910d64af..e4fbab1bab5 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -25102,8 +25102,21 @@ cp_parser_parameter_declaration_clause (cp_parser* parser, committed yet, nor should we. Pushing here will detect the error of redefining a parameter. */ if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) - for (tree p : pending_decls) - pushdecl (p); + { + for (tree p : pending_decls) + pushdecl (p); + + /* Delayed checking of auto parameters. */ + if (!parser->auto_is_implicit_function_template_parm_p + && cxx_dialect >= cxx14) + for (tree p = parameters; p; p = TREE_CHAIN (p)) + if (type_uses_auto (TREE_TYPE (TREE_VALUE (p)))) + { + error_at (location_of (TREE_VALUE (p)), + "% parameter not permitted in this context"); + TREE_TYPE (TREE_VALUE (p)) = error_mark_node; + } + } /* Finish the parameter list. */ if (!ellipsis_p) diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast15.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast15.C new file mode 100644 index 00000000000..deb1efcc46c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast15.C @@ -0,0 +1,21 @@ +// PR c++/112482 +// { dg-do compile { target c++23 } } +// { dg-options "-Wno-vexing-parse" } + +void foo (auto i, auto j); + +struct A { + A(int,int); +}; + +void +g (int a) +{ + A b1(auto(42), auto(42)); + A b2(auto(a), auto(42)); + A b3(auto(42), auto(a)); + A b4(auto(a), // { dg-error "13:'auto' parameter" } + auto(a2)); // { dg-error "13:'auto' parameter" } + int v1(auto(42)); + int fn1(auto(a)); // { dg-error "16:'auto' parameter" } +}