In elaborated-type-specifier, the typename keyword can only follow a
nested-name-specifier:

  class-key nested-name-specifier template[opt] simple-template-id

but we weren't detecting it.

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

2018-11-14  Marek Polacek  <pola...@redhat.com>

        PR c++/87781 - detect invalid elaborated-type-specifier.
        * parser.c (cp_parser_elaborated_type_specifier): Ensure that
        typename follows a nested-name-specifier.

        * g++.dg/parse/elab3.C: New test.

diff --git gcc/cp/parser.c gcc/cp/parser.c
index e9e49b15702..0ab44ab93e3 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -17986,6 +17986,10 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
         template-id or not.  */
       if (!template_p)
        cp_parser_parse_tentatively (parser);
+      /* The `template' keyword must follow a nested-name-specifier.  */
+      else if (!nested_name_specifier)
+       return error_mark_node;
+
       /* Parse the template-id.  */
       token = cp_lexer_peek_token (parser->lexer);
       decl = cp_parser_template_id (parser, template_p,
diff --git gcc/testsuite/g++.dg/parse/elab3.C gcc/testsuite/g++.dg/parse/elab3.C
new file mode 100644
index 00000000000..5488d90afcf
--- /dev/null
+++ gcc/testsuite/g++.dg/parse/elab3.C
@@ -0,0 +1,5 @@
+// PR c++/87781
+// { dg-do compile }
+
+template<class> class A;
+class template A<int> *p; // { dg-error "" }

Reply via email to