Bug 7228 - ICE when using member template and template function
ICE when using member template and template function
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c++
3.1.1
: P3 normal
: ---
Assigned To: David Edelsohn
: ice-on-valid-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2002-07-06 23:56 UTC by ndry
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ndry 2002-07-06 23:56:00 UTC
The following code crushes gcc 3.1.1 and 3.0.4 (but not 2.95.4).
-----------------------
#include<iostream>
template<int N>
struct S {
    enum {I = N};
    template<int NN>
    struct rebind {
        typedef S<NN> Type;
    };
};

template<class S,class T>
void f(S& s, T& t)
{
    typename S::rebind<t.I>::Type ss;
    std::cout << ss.I << std::endl; 
}

int main(){ 
    S<0> s; 
    S<1> t; 
    f(s,t); 
}
------------------

$ g++ test.cc
test.cc: In function `void f(S&, T&) [with S = S<0>, T = S<1>]':
test.cc:21:   instantiated from here
test.cc:14: internal error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://www.gnu.org/software/gcc/bugs.html> for instructions.

Also fails without iostream:

template<int N>
struct S {
  enum {I = N};
  template<int NN>
  struct rebind {
    typedef S<NN> Type;
  };
};

template<class S,class T>

int f(S& s, T& t)
{
  typename S::rebind<t.I>::Type ss;
  return ss.I;
}

int main(){ 
  S<0> s; 
  S<1> t; 
  f(s,t); 
}

The SEGV occurs in cp/decl.c:check_initializer.  The DECL
type is TYPENAME_TYPE which matches IS_AGGR_TYPE, but it
does not have a TYPE_LANG_SPECIFIC node attached, so
LANG_TYPE_CLASS_CHECK dereferences NULL.

Release:
3.1.1pr and 3.0.4

How-To-Repeat:
Compile the above.
Comment 1 ndry 2002-07-06 23:56:00 UTC
Fix:
Either LANG_TYPE_CLASS_CHECK needs to ensure that
TYPE_LANG_SPECIFIC != NULL or the DECL type needs to have
a TYPE_LANG_SPECIFIC node attached.

        * cp/decl.c (check_initializer): Ensure lang_type structure exists
        before accessing with CLASSTYPE macros.

Index: decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.952
diff -c -p -r1.952 decl.c
*** decl.c      21 Oct 2002 16:47:12 -0000      1.952
--- decl.c      24 Oct 2002 02:35:38 -0000
*************** check_initializer (tree decl, tree init,
*** 8062,8070 ****
      {
        tree core_type = strip_array_types (type);

!       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
        error ("structure `%D' with uninitialized const members", decl);
!       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
        error ("structure `%D' with uninitialized reference members",
               decl);

--- 8062,8072 ----
      {
        tree core_type = strip_array_types (type);

!       if (TYPE_LANG_SPECIFIC (core_type)
!         && CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
        error ("structure `%D' with uninitialized const members", decl);
!       if (TYPE_LANG_SPECIFIC (core_type)
!         && CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
        error ("structure `%D' with uninitialized reference members",
               decl);
Comment 2 Nathan Sidwell 2002-09-14 13:04:18 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed. 2.96RH also ices, I cannot confirm that 2.95.4
    is ok -- possible regressioin
Comment 3 David Edelsohn 2002-10-24 10:45:13 UTC
Responsible-Changed-From-To: unassigned->dje
Responsible-Changed-Why: looks helpful
Comment 4 David Edelsohn 2002-10-25 08:20:58 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed in GCC 3.2.1 and GCC 3.3
Comment 5 David Edelsohn 2002-10-25 15:13:06 UTC
From: dje@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/7228
Date: 25 Oct 2002 15:13:06 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	dje@gcc.gnu.org	2002-10-25 08:13:06
 
 Modified files:
 	gcc/cp         : ChangeLog class.c cp-tree.h typeck2.c 
 
 Log message:
 	PR c++/7228
 	* cp-tree.h (CLASSTYPE_READONLY_FIELDS_NEED_INIT): Check that
 	lang_type structure exists before accessing field.
 	(SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT): New macro.
 	(CLASSTYPE_REF_FIELDS_NEED_INIT): Similar.
 	(SET_CLASSTYPE_REF_FIELDS_NEED_INIT): New macro.
 	* class.c (check_field_decls): Use new macros.
 	* typeck2.c (process_init_constructor): Remove redundant check for
 	existence of lang_type structure.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3026&r2=1.3027
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/class.c.diff?cvsroot=gcc&r1=1.485&r2=1.486
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&r1=1.762&r2=1.763
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&r1=1.130&r2=1.131
 
Comment 6 David Edelsohn 2002-10-25 15:20:31 UTC
From: dje@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/7228
Date: 25 Oct 2002 15:20:31 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	dje@gcc.gnu.org	2002-10-25 08:20:30
 
 Modified files:
 	gcc/cp         : ChangeLog class.c cp-tree.h typeck2.c 
 
 Log message:
 	PR c++/7228
 	* cp-tree.h (CLASSTYPE_READONLY_FIELDS_NEED_INIT): Check that
 	lang_type structure exists before accessing field.
 	(SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT): New macro.
 	(CLASSTYPE_REF_FIELDS_NEED_INIT): Similar.
 	(SET_CLASSTYPE_REF_FIELDS_NEED_INIT): New macro.
 	* class.c (check_field_decls): Use new macros.
 	* typeck2.c (process_init_constructor): Remove redundant check for
 	existence of lang_type structure.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.2685.2.114.2.32&r2=1.2685.2.114.2.33
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/class.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.436.2.10.2.8&r2=1.436.2.10.2.9
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.681.2.12.2.6&r2=1.681.2.12.2.7
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.117.2.2&r2=1.117.2.2.2.1