c++: DR2237, cdtor and template-id tweaks [PR107126]
commitcff174fabd6c980c09aee95db1d9d5c22421761f
authorMarek Polacek <polacek@redhat.com>
Fri, 2 Feb 2024 19:53:01 +0000 (2 14:53 -0500)
committerMarek Polacek <polacek@redhat.com>
Sat, 10 Feb 2024 13:22:59 +0000 (10 08:22 -0500)
tree0adbe61835b14e2246101a6d5349e5f70d97ae5e
parentf88219333e85a05a98468f67d2f2190fc330044e
c++: DR2237, cdtor and template-id tweaks [PR107126]

Since my r11-532 changes to implement DR2237, for this test:

  template<typename T>
  struct S {
    S<T>();
  };

in C++20 we emit the ugly:

q.C:3:8: error: expected unqualified-id before ')' token
    3 |   S<T>();

which doesn't explain what the problem is.  This patch improves that
diagnostic, reduces the error to a pedwarn, and adds a -Wc++20-compat
diagnostic.  We now say:

q.C:3:7: warning: template-id not allowed for constructor in C++20 [-Wtemplate-id-cdtor]
    3 |   S<T>();
q.C:3:7: note: remove the '< >'

This patch also fixes
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97202#c8>
where the C++20 diagnostic was missing altogether:  The problem was that I checked
for CPP_TEMPLATE_ID too early, at a point at which cp_parser_template_id may not
have been called yet.  So let's check for it at the end of the function, after
the tentative parse and rollback.

-Wc++20-compat triggered in libitm/; I sent a patch for that.

DR 2237
PR c++/107126
PR c++/97202

gcc/c-family/ChangeLog:

* c-opts.cc (c_common_post_options): In C++20 or with -Wc++20-compat,
turn on -Wtemplate-id-cdtor.
* c.opt (Wtemplate-id-cdtor): New.

gcc/cp/ChangeLog:

* parser.cc (cp_parser_unqualified_id): Downgrade the DR2237 error to
a pedwarn.
(cp_parser_constructor_declarator_p): Likewise.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wtemplate-id-cdtor.

gcc/testsuite/ChangeLog:

* g++.dg/DRs/dr2237.C: Adjust dg-error.
* g++.dg/parse/constructor2.C: Likewise.
* g++.dg/template/error34.C: Likewise.
* g++.old-deja/g++.pt/ctor2.C: Likewise.
* g++.dg/DRs/dr2237-2.C: New test.
* g++.dg/DRs/dr2237-3.C: New test.
* g++.dg/DRs/dr2237-4.C: New test.
* g++.dg/DRs/dr2237-5.C: New test.
* g++.dg/warn/Wtemplate-id-cdtor-1.C: New test.
* g++.dg/warn/Wtemplate-id-cdtor-2.C: New test.
* g++.dg/warn/Wtemplate-id-cdtor-3.C: New test.
* g++.dg/warn/Wtemplate-id-cdtor-4.C: New test.
16 files changed:
gcc/c-family/c-opts.cc
gcc/c-family/c.opt
gcc/cp/parser.cc
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/DRs/dr2237-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2237-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2237-4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2237-5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2237.C
gcc/testsuite/g++.dg/parse/constructor2.C
gcc/testsuite/g++.dg/template/error34.C
gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-4.C [new file with mode: 0644]
gcc/testsuite/g++.old-deja/g++.pt/ctor2.C