Bug 30897 - [4.2 regression] ICE with default argument in template template parameter
Summary: [4.2 regression] ICE with default argument in template template parameter
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P1 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code, monitored
Depends on:
Blocks:
 
Reported: 2007-02-20 21:24 UTC by Volker Reichelt
Modified: 2009-03-30 21:38 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0
Known to fail: 4.2.5
Last reconfirmed: 2007-10-29 19:17:28


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2007-02-20 21:24:43 UTC
The following valid code snippet triggers an ICE since GCC 3.1:

====================================================================
template<template <typename T, typename = T > class U> struct A
{
  template<int> U<int> foo();
};
====================================================================

bug.cc:3: internal compiler error: in tsubst, at cp/pt.c:7450
Please submit a full bug report, [etc.]

This is related to PR 30044, but here we have a regression.
Comment 1 Richard Biener 2007-02-20 21:46:23 UTC
Confirmed.
Comment 2 Mark Mitchell 2007-03-23 00:50:24 UTC
This code in lookup_template_class:

-      /* Consider an example where a template template parameter declared as
-
-          template <class T, class U = std::allocator<T> > class TT
-
-        The template parameter level of T and U are one level larger than
-        of TT.  To proper process the default argument of U, say when an
-        instantiation `TT<int>' is seen, we need to build the full
-        arguments containing {int} as the innermost level.  Outer levels,
-        available when not appearing as default template argument, can be
-        obtained from `current_template_args ()'.
-
-        Suppose that TT is later substituted with std::vector.  The above
-        instantiation is `TT<int, std::allocator<T> >' with TT at
-        level 1, and T at level 2, while the template arguments at level 1
-        becomes {std::vector} and the inner level 2 is {int}.  */
-
-      if (current_template_parms)
-       arglist = add_to_template_args (current_template_args (), arglist);

is simply bogus; the current template arguments have nothing to do with the situation.  It is this code which causes the crash.

I believe that the correct change is to remove that code and to make template parameters for template template parameters restart at level 1.  So, in:

  template <template <typename T> class A> 
  class B {};

both T and A have level 1.  (Right now, T has level 2.)

T should have level 1 because, when instantiating A, that's what make sense.
Comment 3 Paolo Carlini 2007-09-23 11:52:49 UTC
By the way, the hunk of code at issue dates back to this time:

  http://gcc.gnu.org/ml/gcc-patches/2001-10/msg00589.html

(and, by the way, I cannot find an explicit approval in the mailing list).

Anyway, the effect of removing it is fixing this PR and regressing only on the testcase committed with it (template/ttp2.C).
Comment 4 Jason Merrill 2007-11-02 05:50:23 UTC
Subject: Bug 30897

Author: jason
Date: Fri Nov  2 05:50:06 2007
New Revision: 129844

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129844
Log:
        PR c++/30897
        * pt.c (push_template_decl_real): Set DECL_CONTEXT on template
        template parms.
        (lookup_template_class): Use it to get the outer template args
        for instantiating one.

        PR c++/29236
        * pt.c (reduce_template_parm_level): tsubst the parameters
        of a template template parm.

Added:
    trunk/gcc/testsuite/g++.dg/template/ttp23.C
    trunk/gcc/testsuite/g++.dg/template/ttp24.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c

Comment 5 Jason Merrill 2007-11-02 05:58:18 UTC
Fixed for 4.3, though  not as Mark suggested; we need to have the arguments of the enclosing templates in order to bind names in the default argument properly.
Comment 6 Joseph S. Myers 2008-07-04 21:58:57 UTC
Closing 4.1 branch.
Comment 7 Joseph S. Myers 2009-03-30 21:38:11 UTC
Closing 4.2 branch, fixed in 4.3.