]> gcc.gnu.org Git - gcc.git/commit
c++: fix parsing with auto(x) [PR112410]
authorMarek Polacek <polacek@redhat.com>
Thu, 9 Nov 2023 17:25:25 +0000 (12:25 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 16 Nov 2023 03:22:20 +0000 (22:22 -0500)
commit70060dadfbf0d0af5f4cab5f3aff3223a4523606
tree8197907f6b80bf58e65078ab6be19c76ffe7ce7d
parent9251db0dabc8e75c31b49a5b589124e9a2bc8299
c++: fix parsing with auto(x) [PR112410]

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
gcc/testsuite/g++.dg/cpp23/auto-fncast13.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp23/auto-fncast14.C [new file with mode: 0644]
This page took 0.063262 seconds and 5 git commands to generate.