c++: template keyword accepted before destructor names [PR94336]

Message ID 20200326223159.162421-1-polacek@redhat.com
State New
Headers show
Series
  • c++: template keyword accepted before destructor names [PR94336]
Related show

Commit Message

Christophe Lyon via Gcc-patches March 26, 2020, 10:31 p.m.
This came up on the C++ core list recently.  We don't diagnose the case
when 'template' is followed by a destructor name which is not permitted
by [temp.names]/5.

	PR c++/94336 - template keyword accepted before destructor names.
	* parser.c (cp_parser_unqualified_id): Give an error when 'template'
	is followed by a destructor name.

	* g++.dg/template/template-keyword2.C: New test.
---
 gcc/cp/parser.c                                   | 9 +++++++++
 gcc/testsuite/g++.dg/template/template-keyword2.C | 5 +++++
 2 files changed, 14 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/template-keyword2.C


base-commit: 16948c54b7576fb4b27c59915eac71a0c6bf94f6
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Comments

Christophe Lyon via Gcc-patches March 27, 2020, 2:06 a.m. | #1
On 3/26/20 6:31 PM, Marek Polacek wrote:
> This came up on the C++ core list recently.  We don't diagnose the case

> when 'template' is followed by a destructor name which is not permitted

> by [temp.names]/5.


OK.

> 	PR c++/94336 - template keyword accepted before destructor names.

> 	* parser.c (cp_parser_unqualified_id): Give an error when 'template'

> 	is followed by a destructor name.

> 

> 	* g++.dg/template/template-keyword2.C: New test.

> ---

>   gcc/cp/parser.c                                   | 9 +++++++++

>   gcc/testsuite/g++.dg/template/template-keyword2.C | 5 +++++

>   2 files changed, 14 insertions(+)

>   create mode 100644 gcc/testsuite/g++.dg/template/template-keyword2.C

> 

> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c

> index 05363653691..8c5fc5eea09 100644

> --- a/gcc/cp/parser.c

> +++ b/gcc/cp/parser.c

> @@ -6068,6 +6068,15 @@ cp_parser_unqualified_id (cp_parser* parser,

>   	      cp_lexer_consume_token (parser->lexer);

>   	    return error_mark_node;

>   	  }

> +	if (template_keyword_p)

> +	  {

> +	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))

> +	      error_at (tilde_loc, "%<template%> keyword not permitted in "

> +			"destructor name");

> +	    cp_parser_simulate_error (parser);

> +	    return error_mark_node;

> +	  }

> +

>   	gcc_assert (!scope || TYPE_P (scope));

>   

>   	token = cp_lexer_peek_token (parser->lexer);

> diff --git a/gcc/testsuite/g++.dg/template/template-keyword2.C b/gcc/testsuite/g++.dg/template/template-keyword2.C

> new file mode 100644

> index 00000000000..27d874ba7d8

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/template/template-keyword2.C

> @@ -0,0 +1,5 @@

> +// PR c++/94336 - template keyword accepted before destructor names.

> +

> +template<typename T> void f(T *p) { p->template ~X(); } // { dg-error ".template. keyword not permitted in destructor name" }

> +template<typename T> struct X {};

> +void g(X<int> *p) { f(p); }

> 

> base-commit: 16948c54b7576fb4b27c59915eac71a0c6bf94f6

>

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 05363653691..8c5fc5eea09 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -6068,6 +6068,15 @@  cp_parser_unqualified_id (cp_parser* parser,
 	      cp_lexer_consume_token (parser->lexer);
 	    return error_mark_node;
 	  }
+	if (template_keyword_p)
+	  {
+	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
+	      error_at (tilde_loc, "%<template%> keyword not permitted in "
+			"destructor name");
+	    cp_parser_simulate_error (parser);
+	    return error_mark_node;
+	  }
+
 	gcc_assert (!scope || TYPE_P (scope));
 
 	token = cp_lexer_peek_token (parser->lexer);
diff --git a/gcc/testsuite/g++.dg/template/template-keyword2.C b/gcc/testsuite/g++.dg/template/template-keyword2.C
new file mode 100644
index 00000000000..27d874ba7d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/template-keyword2.C
@@ -0,0 +1,5 @@ 
+// PR c++/94336 - template keyword accepted before destructor names.
+
+template<typename T> void f(T *p) { p->template ~X(); } // { dg-error ".template. keyword not permitted in destructor name" }
+template<typename T> struct X {};
+void g(X<int> *p) { f(p); }