public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/64679] Spurious redefinition error when parsing not-quite-most-vexing-parse declarations
Date: Thu, 05 May 2022 21:34:51 +0000	[thread overview]
Message-ID: <bug-64679-4-Yvf4IiN0DS@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-64679-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64679

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:ee9128189623ce43343ce326caa8d5eec2eccfec

commit r13-146-gee9128189623ce43343ce326caa8d5eec2eccfec
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue May 3 19:01:19 2022 -0400

    c++: wrong error with MVP and pushdecl [PR64679]

    This patch fixes the second half of 64679.  Here we issue a wrong
    "redefinition of 'int x'" for the following:

      struct Bar {
        Bar(int, int, int);
      };

      int x = 1;
      Bar bar(int(x), int(x), int{x}); // #1

    cp_parser_parameter_declaration_list does pushdecl every time it sees
    a named parameter, so the second "int(x)" causes the error.  That's
    premature, since this turns out to be a constructor call after the
    third argument!

    If the first parameter is parenthesized, we can't push until we've
    established we're looking at a function declaration.  Therefore this
    could be fixed by some kind of lookahead.  I thought about introducing a
    lightweight variant of cp_parser_parameter_declaration_list that would
    not have any side effects and would return as soon as it figures out
    whether it's looking at a declaration or expression.  Since that would
    require fairly nontrivial changes, I wanted something simpler.

    Something like delaying the pushdecl until we've reached the ')'
    following the parameter-declaration-clause.  But we must push the
    parameters before processing a default argument, as in:

      Bar bar(int(a), int(b), int c = sizeof(a));  // valid

    Moreover, this code should still be accepted

      Bar f(int(i), decltype(i) j = 42);

    so this patch stashes parameters into a vector when parsing tentatively
    only when pushdecl-ing a parameter would result in a clash and an error
    about redefinition/redeclaration.  The stashed parameters are pushed at
    the end of a parameter-declaration-clause if it's followed by a ')', so
    that we still diagnose redefining a parameter.

            PR c++/64679

    gcc/cp/ChangeLog:

            * parser.cc (cp_parser_parameter_declaration_clause): Maintain
            a vector of parameters that haven't been pushed yet.  Push them at
the
            end of a valid parameter-declaration-clause.
            (cp_parser_parameter_declaration_list): Take a new auto_vec
parameter.
            Do not pushdecl while parsing tentatively when pushdecl-ing a
parameter
            would result in a hard error.
            (cp_parser_cache_defarg): Adjust the call to
            cp_parser_parameter_declaration_list.

    gcc/testsuite/ChangeLog:

            * g++.dg/parse/ambig11.C: New test.
            * g++.dg/parse/ambig12.C: New test.
            * g++.dg/parse/ambig13.C: New test.
            * g++.dg/parse/ambig14.C: New test.

  parent reply	other threads:[~2022-05-05 21:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-20  1:30 [Bug c++/64679] New: " rs2740 at gmail dot com
2022-04-29 19:06 ` [Bug c++/64679] " mpolacek at gcc dot gnu.org
2022-05-04 20:06 ` cvs-commit at gcc dot gnu.org
2022-05-05 21:34 ` cvs-commit at gcc dot gnu.org [this message]
2022-05-05 21:37 ` mpolacek at gcc dot gnu.org

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=bug-64679-4-Yvf4IiN0DS@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).