public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: further DR 2237 fix [PR97202]
@ 2024-02-06  3:12 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2024-02-06  3:12 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

Technically, not a regression.  But it's such a simple fix for such
rare code that I think we should put it in now and be done with
DR 2237.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
With a redundant inline specifier like this:

  template<typename Base> struct S : Base {
    inline S<Base>() {}
  };

we don't detect the simple-template-id as the declarator-id of the
constructor.  The problem is that I check 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.

	PR c++/97202

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_constructor_declarator_p): Check CPP_TEMPLATE_ID
	at the end of the function.

gcc/testsuite/ChangeLog:

	* g++.dg/DRs/dr2237-5.C: New test.
---
 gcc/cp/parser.cc                    | 4 +---
 gcc/testsuite/g++.dg/DRs/dr2237-5.C | 7 +++++++
 2 files changed, 8 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/DRs/dr2237-5.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 473eaf4f1f7..8befd26feab 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -32337,8 +32337,6 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
       && next_token->type != CPP_TEMPLATE_ID)
     return false;
 
-  const bool saw_template_id = (next_token->type == CPP_TEMPLATE_ID);
-
   /* Parse tentatively; we are going to roll back all of the tokens
      consumed here.  */
   cp_parser_parse_tentatively (parser);
@@ -32558,7 +32556,7 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
   /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
      declarator-id of a constructor or destructor.  */
   if (constructor_p
-      && saw_template_id
+      && cp_lexer_peek_token (parser->lexer)->type == CPP_TEMPLATE_ID
       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
     {
       auto_diagnostic_group d;
diff --git a/gcc/testsuite/g++.dg/DRs/dr2237-5.C b/gcc/testsuite/g++.dg/DRs/dr2237-5.C
new file mode 100644
index 00000000000..fd51968f7e1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/DRs/dr2237-5.C
@@ -0,0 +1,7 @@
+// PR c++/97202
+// { dg-options "" }
+
+template<typename Base> struct S : Base {
+  inline S<Base>() {} // { dg-warning "template-id not allowed for constructor" "" { target c++20 } }
+  inline ~S<Base>() {} // { dg-warning "template-id not allowed for destructor" "" { target c++20 } }
+};

base-commit: c5d34912ad576be1ef19be92f7eabde54b9089eb
prerequisite-patch-id: 2987a013eda8bc2ca9d8373eedac82067a654a5c
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-06  3:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-06  3:12 [PATCH] c++: further DR 2237 fix [PR97202] Marek Polacek

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).