Skip to content
Snippets Groups Projects
Commit 60979215 authored by Marek Polacek's avatar Marek Polacek
Browse files

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.

(cherry picked from commit 70060dad)
parent 08f4496a
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment