From patchwork Wed Nov 15 22:24:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 165627 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2846310vqg; Wed, 15 Nov 2023 14:24:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBI210G9CiBRkVBJ9LwK8Xpmjht3Ks0p1H/tNDhFPHjYbhUD/LrmSx8tLU4FuBALyQwoaM X-Received: by 2002:a0d:c982:0:b0:598:5bb5:1801 with SMTP id l124-20020a0dc982000000b005985bb51801mr13769960ywd.50.1700087077191; Wed, 15 Nov 2023 14:24:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700087077; cv=pass; d=google.com; s=arc-20160816; b=Qv1MLx/L5opios4veVXEcpy9zCwyu6OQHq2UulaIwRyQl9VFtM86JgzYVNJfyrxI43 EMV9GECpXhtSjKlZdqQBht39DQkdQy2jntN2DeITylQ8US7gpay/rvebbMqqojLH3RUY ZPnLA/Xg4gC+w1dig6X+3j3Df7/S2NIspS30mxiGgXQzP+Y/TDAm/tIU3eUjlxTkVcXc oqdtpBDNYXYqIgwMM+WEpLHuEeB9AeBtzzeyH02u3JeiyJZHxWRUJuHYTApdBpJg7i/z eVdM27pAsz+XZfaKvBw+qEi1bnMd7b4ATq4zoa5oFuKz1DyR9xe6j7e7wqQfB7ODK6T+ JwRg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition:user-agent :in-reply-to:mime-version:references:message-id:subject:cc:to:from :date:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=S/0b/TBLUl4Sz/t+9c2ZxwtQMRuQEPwAJnWo1e0VY/U=; fh=rsfJLMIL/7j+58qzkgIXBlfQSah3iXypQc76SN5PqTs=; b=IDf/W4vULcdO1pWth5raPWvkO+/s1Evp/W1pNCQuQumZdTZ5QpHtReKNAn4QShtKsc i3Tl2srwW8d96dWyudXHPApL/RuxTvlFKW2M4+QbNEU7q7NtNLICgZldyeQHy1jxtunH j6PKSUznZgHnoa53hA4EyCjsr2s2IonB3MesbQO1C4IpRGPEXfvZ8wYYfA883mJyhc+j tnCpeVL3KrAxVmHFgqpzL8xmoQBkhzORyJIcRakbR88IvU6fu3df9Oqci6cF2ylpZbf3 +se9EW2rMgk+iIuIGNwZhG87V6nXQfLHJKDX7KFozoieQBecAFikKw2JigLuOWJ6kKxF kqxw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HgDiuVI1; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l6-20020a056214104600b00670f2a1a0a4si9632045qvr.310.2023.11.15.14.24.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 14:24:37 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HgDiuVI1; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EDF4F385842A for ; Wed, 15 Nov 2023 22:24:36 +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 6F7633858D32 for ; Wed, 15 Nov 2023 22:24:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F7633858D32 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 6F7633858D32 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=1700087052; cv=none; b=oZtvMaQF2I3VJhglBzkUAhX2HSysTK2/Q9JbmSLrF6eyGEOgz5zsEYmu/VtVVExH0RtJyY+CcDkgZQ129P9l4s094NilUH3KR2B8c8glnbVowSZVPNaA/Ha5rbOMRdVN3vCAu4c6ZBRiCmJCJkgvJ41MIkeVusoe11wKP2xLzs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700087052; c=relaxed/simple; bh=XsmAsII2Yvnj7Uqm48v8DbTCsEFDcIPZcxXkoXEpwxY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=bVxLVwxjPV6RivesvTBpSaXAt+MsWq3/N56fJEoT3UHh1R1pFM/Zp5U9/FiZVy0hSCLUsSZLt5Te6O+0rUnXhojO7aYomknq11a+EAPGwG7QTjSLkMgadkqd8I+aNzxhnkVoLXXARD2uvZpSGUxaQEsWs5jpoA4xx/V6uT+0UgM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700087050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=S/0b/TBLUl4Sz/t+9c2ZxwtQMRuQEPwAJnWo1e0VY/U=; b=HgDiuVI1JRu7KRVuU1SGOM75zCyavGyz07crnGk0qd0w+7Hz1lJ2QbxWFO4BGgDclfUfxT LZU69ytebV+BNXP4DMsmH/vujQDlFEyzdLLG/IcYBD1xQrtUB7CN9/QSPqHyZ5MM3+gnM8 ZIv784FL53+2+/c+lD/xVDfNeQS3FT8= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-326-rhh7FbSaM2ePwbdCrXOIfA-1; Wed, 15 Nov 2023 17:24:08 -0500 X-MC-Unique: rhh7FbSaM2ePwbdCrXOIfA-1 Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-587abe0ab5cso133938eaf.3 for ; Wed, 15 Nov 2023 14:24:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700087048; x=1700691848; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S/0b/TBLUl4Sz/t+9c2ZxwtQMRuQEPwAJnWo1e0VY/U=; b=mmucUMVOdYlrKc1RlP6miiVSmLgQIgVrm6Z/+jyUhupuGH4p3DENx2TOU7Yx7Gs2eC oSLL1EsDUiDSIqPCNJVzn6m3gzYN0q7s4rduoDRKMXSDe2aoNx2bh2tn6cdpnim9gzuK R5Y3ua332Q+csoVxccCYtZ9XfzcSWVbZaDSJewmbbDl5uzZHzGibmJq+87tG7V7KASSE JW7WFuv/f2IFlILvB6aGrkdPzsLhTyU2vRIZrpxf2pIKBEWbnfNiUwtpSp8Ar9Gix9p+ SayhZLLtm63P9cJQ3P2rcki0n4B8ZXSW4DnOB/Y0NpK/fjgwCnuvzcSwZng05wG538w8 6Eow== X-Gm-Message-State: AOJu0YygsavM+YKU7DIB2kb0+HOLdthXBeLXdSxovHJ8YxTRhtKzFlvY NJUT5mnYKLdDDdY7tUTA+2oKZHoIY6PQFHO3xaWamqTHDSQHrxlqo14J9RyRbmeMVinm4AFcrJp VF/BUP++JYUqYI43MTw== X-Received: by 2002:a05:6358:919e:b0:168:e68f:ad59 with SMTP id j30-20020a056358919e00b00168e68fad59mr6715490rwa.2.1700087047818; Wed, 15 Nov 2023 14:24:07 -0800 (PST) X-Received: by 2002:a05:6358:919e:b0:168:e68f:ad59 with SMTP id j30-20020a056358919e00b00168e68fad59mr6715473rwa.2.1700087047366; Wed, 15 Nov 2023 14:24:07 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id l19-20020ac84a93000000b0041b016faf7esm3854869qtq.58.2023.11.15.14.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 14:24:06 -0800 (PST) Date: Wed, 15 Nov 2023 17:24:05 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v3] c++: fix parsing with auto(x) [PR112410] Message-ID: References: <20231109195835.429291-1-polacek@redhat.com> <1c680274-040d-40d7-b998-b100084c0ba5@redhat.com> <5b947ae5-b4bb-4bf2-91b5-6dcace7b537d@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline 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_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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782117773422251526 X-GMAIL-MSGID: 1782670507116128242 On Tue, Nov 14, 2023 at 05:27:03PM -0500, Jason Merrill wrote: > On 11/14/23 10:58, Marek Polacek wrote: > > On Mon, Nov 13, 2023 at 09:26:41PM -0500, Jason Merrill wrote: > > > On 11/10/23 20:13, Marek Polacek wrote: > > > > On Thu, Nov 09, 2023 at 07:07:03PM -0500, Jason Merrill wrote: > > > > > On 11/9/23 14:58, Marek Polacek wrote: > > > > > > 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. > > > > > > > > > > Yeah, this is a most vexing parse problem. The code is synthesizing > > > > > template parameters before we've resolved whether the auto is a > > > > > decl-specifier or not. > > > > > > > > > > > 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); > > > > > > > > > > But it doesn't seem to handle the extremely vexing > > > > > > > > > > struct A { > > > > > A(int,int); > > > > > }; > > > > > > > > > > int main() > > > > > { > > > > > int a; > > > > > A b(auto(a), 42); > > > > > } > > > > > > > > Argh. This test should indeed be accepted and is currently rejected, > > > > but it's a different problem: 'b' is at block scope and you can't > > > > have a template there. But when I put it into a namespace scope, > > > > it shows that my patch doesn't work correctly. I've added auto-fncast14.C > > > > for the latter and opened c++/112482 for the block-scope problem. > > > > > I think we need to stop synthesizing immediately when we see RID_AUTO, and > > > > > instead go back after we successfully parse a declaration and synthesize for > > > > > any autos we saw along the way. :/ > > > > > > > > That seems very complicated :(. I had a different idea though; how > > > > about the following patch? The idea is that if we see that parsing > > > > the parameter-declaration-list didn't work, we undo what synthesize_ > > > > did, and let cp_parser_initializer parse "(auto(42))", which should > > > > succeed. I checked that after cp_finish_decl y is initialized to 42. > > > > > > Nice, that's much simpler. Do you also still need the changes to > > > cp_parser_simple_type_specifier? > > > > I do, otherwise we parse > > > > int f (auto{42}); > > > > just as if it had been > > > > int f (auto); > > > > because the {42} is consumed in the cp_parser_simple_type_specifier/RID_AUTO > > loop. :/ > > It isn't consumed there, that loop is just scanning forward to see if > there's a ->. The { is still the next token when we expect it to be a > closing ) in cp_parser_direct_declarator: Ok, the tokens are rolled back after consuming so we can... > > /* Parse the parameter-declaration-clause. */ > > params > > = cp_parser_parameter_declaration_clause (parser, flags); > > const location_t parens_end > > = cp_lexer_peek_token (parser->lexer)->location; > > > > /* Consume the `)'. */ > > parens.require_close (parser); > > Maybe we want to abort_fully_implicit_template here rather than in > cp_parser_parameter_declaration_clause? ...do this instead. Much better. 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. This patch rectifies that by undoing the implicit function template modification. In the test above, we should notice that the parameter list is ill-formed, and since we've synthesized an implicit template parameter, we undo it by calling abort_fully_implicit_template. Then, we'll parse the "(auto(42))" as an initializer. PR c++/112410 gcc/cp/ChangeLog: * parser.cc (cp_parser_direct_declarator): Maybe call abort_fully_implicit_template if it turned out the parameter list was ill-formed. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast13.C: New test. * g++.dg/cpp23/auto-fncast14.C: New test. --- gcc/cp/parser.cc | 13 +++++ gcc/testsuite/g++.dg/cpp23/auto-fncast13.C | 61 ++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp23/auto-fncast14.C | 9 ++++ 3 files changed, 83 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast13.C create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast14.C base-commit: 01bc30b222a9d2ff0269325d9e367f8f1fcef942 diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 5116bcb78f6..d1104336215 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -23594,6 +23594,19 @@ cp_parser_direct_declarator (cp_parser* parser, /* Consume the `)'. */ parens.require_close (parser); + /* For code like + int x(auto(42)); + A a(auto(i), 42); + we have synthesized an implicit template parameter and marked + what we thought was a function as an implicit function template. + But now, having seen the whole parameter list, we know it's not + a function declaration, so undo that. */ + if (cp_parser_error_occurred (parser) + && parser->fully_implicit_function_template_p + /* Don't do this for the inner (). */ + && parser->default_arg_ok_p) + abort_fully_implicit_template (parser); + /* If all went well, parse the cv-qualifier-seq, ref-qualifier and the exception-specification. */ if (member_p || cp_parser_parse_definitely (parser)) 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; +} diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast14.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast14.C new file mode 100644 index 00000000000..9e7a06c87d5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast14.C @@ -0,0 +1,9 @@ +// PR c++/112410 +// { dg-do compile { target c++23 } } + +struct A { + A(int,int); +}; + +int a; +A b1(auto(a), 42);