From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 27A40384F020 for ; Thu, 14 Dec 2023 21:08:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27A40384F020 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 27A40384F020 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=1702588084; cv=none; b=ZfEeqtxGbEOwvFIw0QOG6u9KKoEi3XVZOPmoBBzaYLAUkjIvEBNzNZoLH/7zy6lpUrM+TTdzAP5bcUKHrpzzjf8WgXZfonGTwRj5qgPiGWrPYBcUiVCvtWg4PGOB5iWeFF66GuQf1mfMxJSyAGhMUzJM9m1Eu/wyxgjGGdeGtMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702588084; c=relaxed/simple; bh=6zpHC3oGMbfvZhInMRM2/bH5sHyIt1ODigYs/wYg090=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=UOHFk3WfYQwu5Oq/dFtu7HLUCpbSwFA/g6gSchEPtMVyHMPgACRx7Qtkn4xIjGEz4zJIkCiNYOnTlWhsHIjCHQpyR1Cxj1kNdMYrMJMpOaBX6RIFzVXWP8Ux3tEpDxiwYOP7w0gcjULAnkrQ83wzPHeAnopgvkK6hTCKzs8VheI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702588082; 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: in-reply-to:in-reply-to:references:references; bh=miYu0zYKSU1mSTQlLIfvet8wxLoPcGjoFp3Y7vC17po=; b=MmMsiTEpMyytg+hAOwhvugMcgwsHPUTSan5vsiLtur7gRjD+xmoGP73DHvYyBhuDV3q3CJ AzryKU/xnqIPNdoIJDxOXd53Eg3GK8Ka7QuS1CuS6CcvhXvlOecWM10rSvvitdBOumWWfO Ktv5qa+Xoo66TLKtReVUhcgRfoXPxNM= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-60-qi0ThPrFNLK5A8nYHcQzVw-1; Thu, 14 Dec 2023 16:08:01 -0500 X-MC-Unique: qi0ThPrFNLK5A8nYHcQzVw-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-67aec8cce57so95377586d6.2 for ; Thu, 14 Dec 2023 13:08:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702588080; x=1703192880; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=miYu0zYKSU1mSTQlLIfvet8wxLoPcGjoFp3Y7vC17po=; b=xA+Y7mjn9W6SkRnGJp07Sx8F0MCbe6ry0kB3II5EZHphsCZVliN6W5wqzHfpk6ZZeo n3QQWBHztig3KoQd9xLdWIiPUkBbCIMSTaWXo5B1c3S5wV4beVMtTp3dV1M9CROho29e VkXInqgjsr956YZImc+AQHkKaP7UptYp2WuKaVUS4vN5rP6rkH2fyIGkyC3J7JxhKhZF tbiZrJoX3n2cY4tx9mC+5D8aDKuK3QvC4sf0AHDf7Q85JHkmn0wMJMW6dinRvJdX+pOU 2cjOWQs6wMrDzpb+IhnQznSd2eurnCyqkS/Qm8T3c0XshDnR+1r6QpMqXSJSogIJ6u/D NNIg== X-Gm-Message-State: AOJu0YxmM3pYQfjE1w2Q9LbczE4saFxXyHKYaEl9e3d17TKa1SqkSzx8 /1tLMP4YIQxJgY+JwH+d6WDJoIGM9TOye4UwPV4Zvet0QJNqUOeNV+MlXKFARd72tq6YeKnWsof 9sZeEftgYmv3DecNWPA== X-Received: by 2002:ad4:5aa2:0:b0:67f:12bb:6c06 with SMTP id u2-20020ad45aa2000000b0067f12bb6c06mr1034968qvg.36.1702588080601; Thu, 14 Dec 2023 13:08:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQABDXgHkgl4iVF0ETSQBM3jZ6kOp5lCc+15UVIx0FpD69Ie1/isGtmIfPGBxAnbjPpzYliw== X-Received: by 2002:ad4:5aa2:0:b0:67f:12bb:6c06 with SMTP id u2-20020ad45aa2000000b0067f12bb6c06mr1034957qvg.36.1702588080328; Thu, 14 Dec 2023 13:08:00 -0800 (PST) Received: from [192.168.1.146] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id o12-20020a056214108c00b0067ef91e94b3sm1656165qvr.11.2023.12.14.13.07.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Dec 2023 13:07:59 -0800 (PST) Message-ID: Date: Thu, 14 Dec 2023 16:07:59 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: fix parsing with auto(x) at block scope [PR112482] To: Marek Polacek , GCC Patches References: <20231214210227.1190177-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20231214210227.1190177-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.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,RCVD_IN_SORBS_WEB,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 List-Id: On 12/14/23 16:02, Marek Polacek wrote: > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >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 > > 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" } > +} > > base-commit: 767e2674875139ac8f354ceee655c1a9561b9779