[C++ PATCH] ICE while reporting template error

scott snyder snyder@fnal.gov
Tue Nov 9 16:23:00 GMT 1999


hi -

This is relevant to gcc as checked out from cvs today (2.96 19991109)
on a i686-pc-linux-gnu platform.

For the following (invalid) input, the compiler crashes:

-------------------------------------------------------------
template <class T> class d0_PList;
template <class T>
bool operator< (const d0_PList<T>& a, const d0_PList<T>& b);

template <class T>
struct d0_PVector
{
  void begin ();
  friend bool operator<  <> (const d0_PVector<T>& a, const d0_PVector<T>& b);
};

void tpvector (d0_PVector<int>& v2)
{
  v2.begin();
}


-------------------------------------------------------------


$ ./cc1plus -quiet x.cc
x.cc: In instantiation of `d0_PVector<int>':
x.cc:14:   instantiated from here
x.cc:9: Internal compiler error.
x.cc:9: Please submit a full bug report.
x.cc:9: See <URL: http://www.gnu.org/software/gcc/faq.html#bugreport > for instructions.


Here's where it's crashing:

Program received signal SIGSEGV, Segmentation fault.
0x82d6964 in dump_template_parms (info=0x4011d7a0, primary=1, flags=1156)
    at ../../../src/gcc/cp/error.c:1359
1359          parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;


#0  0x82d6964 in dump_template_parms (info=0x4011d7a0, primary=1, flags=1156)
    at ../../../src/gcc/cp/error.c:1359
#1  0x82d6715 in dump_function_name (t=0x4011e400, flags=1156)
    at ../../../src/gcc/cp/error.c:1290
#2  0x82d5efe in dump_function_decl (t=0x4011e400, flags=1156)
    at ../../../src/gcc/cp/error.c:1159
#3  0x82d534d in dump_decl (t=0x4011e400, flags=1156)
    at ../../../src/gcc/cp/error.c:968
#4  0x82d9f84 in decl_to_string (decl=0x4011e400, verbose=0)
    at ../../../src/gcc/cp/error.c:2215
#5  0x82ac273 in cp_thing (errfn=0x8049c40 <error_with_file_and_line>, 
    atarg1=1, 
    format=0x8368280 "template-id `%D' for `%+D' does not match any template declaration", ap=0xbffff010) at ../../../src/gcc/cp/errfn.c:132
#6  0x82ac591 in cp_error_at (
    format=0x8368280 "template-id `%D' for `%+D' does not match any template declaration") at ../../../src/gcc/cp/errfn.c:312
#7  0x82ae376 in determine_specialization (template_id=0x4011f260, 
    decl=0x4011e400, targs_out=0xbffff068, need_member_template=0)
    at ../../../src/gcc/cp/pt.c:1078
#8  0x82b4057 in tsubst_friend_function (decl=0x4011ba80, args=0x4011d900)
    at ../../../src/gcc/cp/pt.c:4403
#9  0x82b511e in instantiate_class_template (type=0x4011bb00)
    at ../../../src/gcc/cp/pt.c:5013
#10 0x82efa10 in complete_type (type=0x4011bb00)
    at ../../../src/gcc/cp/typeck.c:160
#11 0x829bbdd in lookup_name_real (name=0x4011c5c0, prefer_type=0, nonclass=0, 
    namespaces_only=0) at ../../../src/gcc/cp/decl.c:5576
#12 0x829c088 in lookup_name (name=0x4011c5c0, prefer_type=-2)
    at ../../../src/gcc/cp/decl.c:5722
#13 0x82eece2 in yylex () at ../../../src/gcc/cp/spew.c:292
#14 0x82e2adc in yyparse () at /usr/lib/bison.simple:431
#15 0x804b58b in compile_file (name=0x40117560 "x.cc")
    at ../../src/gcc/toplev.c:3213
#16 0x804f358 in main (argc=2, argv=0xbffffa44) at ../../src/gcc/toplev.c:5578



  else if (primary)
    {
      tree tpl = TI_TEMPLATE (info);
      tree parms = DECL_TEMPLATE_PARMS (tpl);
      int len, ix;

      parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
      len = parms ? TREE_VEC_LENGTH (parms) : 0;
      


Here, the value of parms we get from DECL_TEMPLATE_PARMS is null,
so that the TREE_CODE test crashes.

I tried to fix this by simply protecting this code against a null value
of params:


1999-11-09  Scott Snyder  <snyder@fnal.gov>

	* error.c (dump_template_parms): Protect against null
	DECL_TEMPLATE_PARMS.

Index: error.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/error.c,v
retrieving revision 1.94
diff -u -p -r1.94 error.c
--- error.c	1999/09/29 17:24:18	1.94
+++ error.c	1999/11/09 23:12:19
@@ -1355,8 +1355,9 @@ dump_template_parms (info, primary, flag
       tree tpl = TI_TEMPLATE (info);
       tree parms = DECL_TEMPLATE_PARMS (tpl);
       int len, ix;
-      
-      parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
+
+      if (parms)
+        parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
       len = parms ? TREE_VEC_LENGTH (parms) : 0;
       
       for (ix = 0; ix != len; ix++)


With this change, the compiler emits a reasonable error message:

$ ./cc1plus -quiet x.cc
x.cc: In instantiation of `d0_PVector<int>':
x.cc:14:   instantiated from here
x.cc:9: template-id `operator <<>' for `operator <<> (const d0_PVector<int> &, const d0_PVector<int> &)' does not match any template declaration


I've checked that this change does not change the results of the
g++ testsuite.


thanks,
sss


More information about the Gcc-patches mailing list