This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] [PR14283] Fix ICE in cp_parser_parse_and_diagnose_invalid_type_name


Hello,

I thought my new code to diagnose invalid typenames were triggered only for
plain identifiers, but it turns out that it can be called also on invalid
TEMPLATE_ID_EXPR (where the lookup failed and the name is non-dependent). This
is a first stop-gap, which adds support for proper error messages in this
situation. It is a clear step forward though, since I want to do more work on
this, and move more error reporting to this routine (see also PR10618).

I also noticed a pasto which was latent (use of parser->scope instead of scope:
the former can be anything when you call the function, the latter is the scope
of the invalid identifier, so it's the one we want to use in the diagnostic),
and fixed it.

For the logs, compiling the testcase on 3.4 we get this:

pr14283.cc:10: error: type `A' is not derived from type `C<
<template-parameter-1-1> >'
pr14283.cc:10: error: ISO C++ forbids declaration of `INVALID' with no type
pr14283.cc:10: error: template-id `INVALID<void>' used as a declarator
pr14283.cc:10: error: expected `;' before "X0"
pr14283.cc:11: error: ISO C++ forbids declaration of `INVALID' with no type
pr14283.cc:11: error: template-id `INVALID<void>' used as a declarator
pr14283.cc:11: error: expected `;' before "X1"

while on mainline we now get:

pr14283.cc:10: error: `INVALID' in class `A' does not name a template
pr14283.cc:11: error: `INVALID' in namespace `N' does not name a template

Tested on i686-pc-linux-gnu with no new regressions, OK for mainline?

Giovanni Bajo


cp/
2004-02-26  Giovanni Bajo  <giovannibajo@gcc.gnu.org>

        PR c++/14283
        * parser.c (cp_parser_diagnose_invalid_type_name): Add support for
        TEMPLATE_ID_EXPR, and sanity check. Fix a pasto in the scope to
        inspect.
        (cp_parser_parse_and_diagnose_invalid_type_name): Remove sanity check.

testsuite/
2004-02-26  Giovanni Bajo  <giovannibajo@gcc.gnu.org>

        PR c++/14283
        * g++.dg/parse/error16.C: New test.



Index: parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.174
diff -c -3 -p -r1.174 parser.c
*** parser.c 19 Feb 2004 03:06:09 -0000 1.174
--- parser.c 26 Feb 2004 02:53:30 -0000
*************** static void
*** 1940,1945 ****
--- 1940,1965 ----
  cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
  {
    tree decl, old_scope;
+   /* If we got here, this cannot be a valid variable declaration, thus
+      the cp_parser_id_expression must have resolved to a plain identifier
+      node or an syntactic template id (not a TYPE_DECL).  */
+   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE
+         || TREE_CODE (id) == TEMPLATE_ID_EXPR, 20030203);
+   if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
+     {
+       /* A template-id whose template name cannot be looked up.  */
+       if (!scope)
+  error ("`%E' does not name a template", TREE_OPERAND (id, 0));
+       else if (TYPE_P (scope))
+  error ("`%E' in class `%T' does not name a template",
+        TREE_OPERAND (id, 0), scope);
+       else if (TREE_CODE (scope) == NAMESPACE_DECL)
+  error ("`%E' in namespace `%E' does not name a template",
+        TREE_OPERAND (id, 0), scope);
+       else
+  abort ();
+       return;
+     }
    /* Try to lookup the identifier.  */
    old_scope = parser->scope;
    parser->scope = scope;
*************** cp_parser_diagnose_invalid_type_name (cp
*** 1950,1956 ****
    if (TREE_CODE (decl) == TEMPLATE_DECL)
      error ("invalid use of template-name `%E' without an argument list",
        decl);
!   else if (!parser->scope)
      {
        /* Issue an error message.  */
        error ("`%E' does not name a type", id);
--- 1970,1976 ----
    if (TREE_CODE (decl) == TEMPLATE_DECL)
      error ("invalid use of template-name `%E' without an argument list",
        decl);
!   else if (!scope)
      {
        /* Issue an error message.  */
        error ("`%E' does not name a type", id);
*************** cp_parser_diagnose_invalid_type_name (cp
*** 1998,2009 ****
       but the identifier does not resolve to a valid type name.  */
    else
      {
!       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
   error ("`%E' in namespace `%E' does not name a type",
!         id, parser->scope);
!       else if (TYPE_P (parser->scope))
   error ("`%E' in class `%T' does not name a type",
!         id, parser->scope);
        else
   abort();
      }
--- 2018,2029 ----
       but the identifier does not resolve to a valid type name.  */
    else
      {
!       if (TREE_CODE (scope) == NAMESPACE_DECL)
   error ("`%E' in namespace `%E' does not name a type",
!         id, scope);
!       else if (TYPE_P (scope))
   error ("`%E' in class `%T' does not name a type",
!         id, scope);
        else
   abort();
      }
*************** cp_parser_parse_and_diagnose_invalid_typ
*** 2043,2053 ****
      }
    if (!cp_parser_parse_definitely (parser))
      return false;
-
-   /* If we got here, this cannot be a valid variable declaration, thus
-      the cp_parser_id_expression must have resolved to a plain identifier
-      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
-   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
    /* Emit a diagnostic for the invalid type.  */
    cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
    /* Skip to the end of the declaration; there's no point in
--- 2063,2068 ----





// { dg-do compile }
// Contributed by Volker Reichelt <reichelt at gcc dot gnu dot org>
// PR c++/14283: ICE in new error reporting routine in the parser

struct A
{};
namespace N
{}

template <typename> struct C
{
    typedef A::template INVALID<void> X0;  // { dg-error "does not name a
template" }
    typedef N::template INVALID<void> X1;  // { dg-error "does not name a
template" }
};



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]