c++: auto in trailing-return-type in parameter [PR117778]mastertrunk
commite6e40cb7459c9b21b291fe28e46cd4ebcd924dff
authorMarek Polacek <polacek@redhat.com>
Wed, 29 Jan 2025 20:58:38 +0000 (29 15:58 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 4 Feb 2025 14:10:46 +0000 (4 09:10 -0500)
tree33ab4733363eee10a7ade6d870a4f49b229ea949
parent53d1f6cdb5a82e859176e854636400faba0bf0bf
c++: auto in trailing-return-type in parameter [PR117778]

This PR describes a few issues, both ICE and rejects-valid, but
ultimately the problem is that we don't properly synthesize the
second auto in:

  int
  g (auto fp() -> auto)
  {
    return fp ();
  }

since r12-5860, which disabled auto_is_implicit_function_template_parm_p
in cp_parser_parameter_declaration after parsing the decl-specifier-seq.

If there is no trailing auto, there is no problem.

So we have to make sure auto_is_implicit_function_template_parm_p is
properly set when parsing the trailing auto.  A complication is that
one can write:

  auto f (auto fp(auto fp2() -> auto) -> auto) -> auto;
                                      ~~~~~~~

where only the underlined auto should be synthesized.  So when we
parse a parameter-declaration-clause inside another
parameter-declaration-clause, we should not enable the flag.  We
have no flags to keep track of such nesting, but I think I can walk
current_binding_level to see if we find ourselves in such an unlikely
scenario.

PR c++/117778

gcc/cp/ChangeLog:

* parser.cc (cp_parser_late_return_type_opt): Maybe override
auto_is_implicit_function_template_parm_p.
(cp_parser_parameter_declaration): Move a make_temp_override below.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/lambda-generic-117778.C: New test.
* g++.dg/cpp2a/abbrev-fn2.C: New test.
* g++.dg/cpp2a/abbrev-fn3.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp1y/lambda-generic-117778.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/abbrev-fn2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/abbrev-fn3.C [new file with mode: 0644]