Bug 9778

Summary: [3.4 regression] ICE with sizeof(expr) in non-type template arg
Product: gcc Reporter: Wolfgang Bangerth <bangerth>
Component: c++Assignee: Jeffrey D. Oldham <oldham>
Status: RESOLVED FIXED    
Severity: normal CC: bangerth, ehrhardt, gcc-bugs, s.bosscher
Priority: P3    
Version: unknown   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Wolfgang Bangerth 2003-02-20 17:16:01 UTC
This started SegFaulting somewhere between 2003-01-12 and
2003-01-30:
-------------------------------
namespace NS {
  template <int N> void foo ();
}

template <int N> struct X {
    int m;
    int g () {
      NS::foo<sizeof(m)>();
    }
};

template class X<2>;
--------------------------
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c y.cc
y.cc: In member function `int X<N>::g() [with int N = 2]':
y.cc:12:   instantiated from here
y.cc:8: internal compiler error: Segmentation fault

Here's a backtrace:
#0  0x4009db33 in strlen () from /lib/libc.so.6
#1  0x08146004 in write_expression (expr=0x4015dfa0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:1985
#2  0x0814659f in write_expression (expr=0x4015dfa0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:2017
#3  0x0814659f in write_expression (expr=0x401965f0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:2017
#4  0x08146dae in write_template_arg (node=0x401965f0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:2133
#5  0x08145dae in write_template_args (args=0x40196690)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:1812
#6  0x08143792 in write_nested_name (decl=0x4019857c)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:841
#7  0x08143361 in write_name (decl=0x4019857c, ignore_local_scope=0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:763


The place where this happens is incidentally the same as
that noted in PR 9749, though that is a regression
introduced long ago. Maybe somewhen in the time frame
noted above some code path just started to use the same
path as that in 9749. If so, then one should be able
to squash both bugs with one patch.

The underlying problem is the following: we segfault
in strlen called from write_expression in this hunk:
      /* If it wasn't any of those, recursively expand the expression.  */
      write_string (operator_name_info[(int) code].mangled_name);

In the testcase
(gdb) fr 1
#1  0x08146004 in write_expression (expr=0x4015dfa0)
    at ../../gcc-3.4-CVS/gcc/cp/mangle.c:1985
(gdb) p code
$5 = IDENTIFIER_NODE
(gdb) p (int)code
$6 = 1
(gdb) p operator_name_info[1]
$7 = {identifier = 0x0, name = 0x0, mangled_name = 0x0, arity = 0}

W.

Release:
unknown

Environment:
present mainline
Comment 1 Wolfgang Bangerth 2003-02-20 17:16:01 UTC
Fix:
Index: cp/pt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/pt.c,v
retrieving revision 1.663
diff -c -p -r1.663 pt.c
*** cp/pt.c	24 Feb 2003 07:43:22 -0000	1.663
--- cp/pt.c	24 Feb 2003 19:35:19 -0000
*************** tsubst_copy_and_build (t, args, complain
*** 8137,8143 ****
  	    if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  	      name = build_nt (TEMPLATE_ID_EXPR,
  			       TREE_OPERAND (name, 0),
! 			       TREE_OPERAND (name, 1));
  	    
  	    function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
  					       name);
--- 8137,8143 ----
  	    if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  	      name = build_nt (TEMPLATE_ID_EXPR,
  			       TREE_OPERAND (name, 0),
! 			       build_expr_from_tree (TREE_OPERAND (name, 1)));
  	    
  	    function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
  					       name);
Index: testsuite/g++.dg/parse/template6.C
===================================================================
RCS file: testsuite/g++.dg/parse/template6.C
diff -N testsuite/g++.dg/parse/template6.C
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/parse/template6.C	24 Feb 2003 19:35:21 -0000
***************
*** 0 ****
--- 1,20 ----
+ // { dg-do compile }
+ 
+ // Copyright (C) 2003 Free Software Foundation, Inc.
+ // Contributed by Wolfgang Bangerth <bangerth@ticam.utexas.edu> 20 Feb 2003.
+ 
+ // PR c++/9778.  Ensure templated functions in other namespaces are
+ // correctly instantiated.
+ 
+ namespace NS {
+   template <int N> void foo ();
+ }
+ 
+ template <int N> struct X {
+   int m;
+   void g () {
+     NS::foo<sizeof(m)>();
+   }
+ };
+ 
+ template class X<2>;
Comment 2 Wolfgang Bangerth 2003-02-20 17:41:38 UTC
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: gcc-gnats@gcc.gnu.org, <gcc-bugs@gcc.gnu.org>, <oldham@codesourcery.com>
Cc:  
Subject: Re: c++/9778 -- causing patch
Date: Thu, 20 Feb 2003 17:41:38 -0600 (CST)

 This patch
 
 > 2003-01-16  Jeffrey D. Oldham  <oldham@codesourcery.com>
 >
 >       * cp-tree.h (tsubst_copy_and_build): New declaration.
 >       * pt.c (tsubst_copy): Remove 'build_expr_from_tree' from comment.
 >       (tsubst_expr): Use 'tsubst_copy_and_build'.  Update initial comment.
 >       (tsubst_copy_and_build): New function.
 
 seems to be causing the ICE in PR 9778. A small testcase is
 ----------------------------
 namespace NS {
   template <int N> void foo ();
 }
 
 template <int N> struct X {
     int m;
     int g () {
       NS::foo<sizeof(m)>();
     }
 };
 
 template class X<2>;
 ---------------------------
 
 Jeffrey, would you mind taking a look? This report seems to be the last 
 that prevents me from building (and then testing again) my library with 
 mainline...
 
 If you are lucky, the fix to this report also fixes PR 9749, see the 
 comment in 9778.
 
 Thanks
   Wolfgang
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth             email:            bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth/
 
 
Comment 3 s.bosscher 2003-02-20 19:43:26 UTC
From: Steven Bosscher <s.bosscher@student.tudelft.nl>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org,
	bangerth@ticam.utexas.edu, gcc-prs@gcc.gnu.org
Cc:  
Subject: Re: c++/9778: [3.4 regression] ICE with sizeof(expr) in non-type
 template arg
Date: Thu, 20 Feb 2003 19:43:26 +0100

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9778
 
 devphil says:
 
 : Search converges between 2003-01-16-trunk (#103) and 2003-01-17-trunk 
 (#104).
 
 
 Line-numbered input is:
 ========================================
      1  namespace NS {
      2    template <int N> void foo ();
      3  }
      4 
      5  template <int N> struct X {
      6      int m;
      7      int g () {
      8        NS::foo<sizeof(m)>();
      9      }
     10  };
     11 
     12  template class X<2>;
     13 
 
 ========================================
 
 
 Diagnostic output follows, from the last compiler tested:
 input: In member function `int X<N>::g() [with int N = 2]':
 input:12:   instantiated from here
 input:8: internal compiler 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.
 
 Compiler output follows, from the last compiler tested:
         .file   "input"
 
 
 Greetz
 Steven
 
 

Comment 4 Christian Ehrhardt 2003-02-23 16:50:28 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: This has been analyzed. Besides I can confirm that this is a regression
    from 3.2, hence priority high.
Comment 5 Jeffrey D. Oldham 2003-02-24 20:39:38 UTC
From: oldham@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/9778
Date: 24 Feb 2003 20:39:38 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	oldham@gcc.gnu.org	2003-02-24 20:39:38
 
 Modified files:
 	gcc/cp         : pt.c ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/parse: template6.C 
 
 Log message:
 	2003-02-24  Jeffrey D. Oldham  <oldham@codesourcery.com>
 	
 	PR c++/9778
 	* cp/pt.c (tsubst_copy_and_build): For a templated function inside a
 	scope, process template arguments.
 	* testsuite/g++.dg/parse/template6.C: New test case.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.663&r2=1.664
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3213&r2=1.3214
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/template6.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 
Comment 6 Jeffrey D. Oldham 2003-02-24 20:54:17 UTC
Responsible-Changed-From-To: unassigned->oldham
Responsible-Changed-Why: To fix the problem.
Comment 7 Jeffrey D. Oldham 2003-02-24 20:54:17 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: The problem was resolved via a patch.