public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v2] c++: fix parsing with auto(x) [PR112410]
Date: Tue, 14 Nov 2023 17:27:03 -0500	[thread overview]
Message-ID: <c6be2f52-15b3-4424-838e-39e307c50ae8@redhat.com> (raw)
In-Reply-To: <ZVOZLP8QzE7c6ad9@redhat.com>

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:

>               /* 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?

Jason


  reply	other threads:[~2023-11-14 22:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 19:58 [PATCH] " Marek Polacek
2023-11-10  0:07 ` Jason Merrill
2023-11-11  1:13   ` [PATCH v2] " Marek Polacek
2023-11-14  2:26     ` Jason Merrill
2023-11-14 15:58       ` Marek Polacek
2023-11-14 22:27         ` Jason Merrill [this message]
2023-11-15 22:24           ` [PATCH v3] " Marek Polacek
2023-11-15 23:18             ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c6be2f52-15b3-4424-838e-39e307c50ae8@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).