Bug 29518 - [4.0 Regression] rejects valid template argument, enums vs templates
Summary: [4.0 Regression] rejects valid template argument, enums vs templates
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P1 normal
Target Milestone: 4.1.2
Assignee: Mark Mitchell
URL:
Keywords: rejects-valid
: 29525 29701 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-10-19 21:16 UTC by W E Brown
Modified: 2007-02-03 20:41 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.0 4.1.2 4.2.0 4.3.0
Known to fail: 4.0.2 4.1.1 4.0.4
Last reconfirmed: 2006-10-19 21:58:50


Attachments
preprocessed version of test case (4.02 KB, text/plain)
2006-10-19 21:22 UTC, W E Brown
Details

Note You need to log in before you can comment on or make changes to this bug.
Description W E Brown 2006-10-19 21:16:39 UTC
Regression from gcc 3.4.4; under gcc 4.1.1 it produces the diagnostic "error: 'N<int>::okay' is not a valid template argument for type 'bool' because it is a non-constant expression" -- and I am informed (by gdr) that the bug is still present in gcc 4.2.0.

The code below makes use of the Boost library; I can also provide the preprocessed version of the same code, if requested, but it's on the order of 1000 lines long:


#include "boost/mpl/assert.hpp"

template< class T >
  struct N
{
  static  bool const  okay  = true;
  BOOST_MPL_ASSERT_MSG( okay, message_goes_here, (T) );
};

int
  main()
{
  N<int>  n;
}
Comment 1 Andrew Pinski 2006-10-19 21:19:39 UTC
Yes we need the preprocessed source.
You can attach it to the bug report instead of just copying and pasting it.
Comment 2 W E Brown 2006-10-19 21:22:56 UTC
Created attachment 12463 [details]
preprocessed version of test case
Comment 3 Andrew Pinski 2006-10-19 21:27:18 UTC
I tried a simple testcase and variations on it (changing (T)(ok) to just ok and such):
template <bool a> class b{};

template <class T> class t
{
  static bool const ok = true;
  b<(T)(ok)> c;
};

int main(void)
{
  t<int> a;
}
Comment 4 gdr@cs.tamu.edu 2006-10-19 21:32:18 UTC
Subject: Re:  incorrect "

On Thu, 19 Oct 2006, pinskia at gcc dot gnu dot org wrote:

| I tried a simple testcase and variations on it (changing (T)(ok) to just ok and
| such):

yes, the parens don't change anything -- I've tried that.
I  attempted to reduce the testcase, but I did not have time.
My first attempt was too broad and did not reproduce the bug, so I
left it to the original form.
Comment 5 Andrew Pinski 2006-10-19 21:42:02 UTC
Reducing through a nice trick of using delta and using 3.3.x as the base compiler.
Comment 6 Andrew Pinski 2006-10-19 21:54:52 UTC
Reduced testcase:

template< bool C > int assertion_failed( int);
template< class > 
struct N
{
  static bool const okay = true;
  enum {
    t = sizeof( assertion_failed<okay>( 0))
  };
};
  main()
{
  N<int> n;
}
Comment 7 gdr@cs.tamu.edu 2006-10-19 21:57:00 UTC
Subject: Re:  diagnostic on correct code; regression from
 3.4.4

On Thu, 19 Oct 2006, pinskia at gcc dot gnu dot org wrote:

| Reduced testcase:

Very nice.

| template< bool C > int assertion_failed( int);
| template< class >
| struct N
| {
|   static bool const okay = true;
|   enum {
|     t = sizeof( assertion_failed<okay>( 0))
|   };
| };

|   main()

missing "int".

| {
|   N<int> n;
| }
Comment 8 Andrew Pinski 2006-10-19 21:58:50 UTC
Note bool has nothing to do with the problem, the following testcase shows that:
template< int C > int assertion_failed( int);
template< class > 
struct N
{
  static int const okay = 1;
  enum {
    t = sizeof( assertion_failed<okay>( 0))
  };
};
int main()
{
  N<int> n;
}
Comment 9 Andrew Pinski 2006-10-19 21:59:45 UTC
(In reply to comment #7)
> missing "int".
That is because we did not error out on it in 3.3.3 or 3.4.0 for that matter, I wonder why.
Comment 10 Andrew Pinski 2006-10-20 07:25:03 UTC
Note I found a related regression in 4.0.4 only and I filed that as PR 29522.
Comment 11 Richard Biener 2006-10-20 14:34:24 UTC
*** Bug 29525 has been marked as a duplicate of this bug. ***
Comment 12 Phil Richards 2006-11-12 11:04:45 UTC
From Aleksey Gurtovoy (co-author Boost.MPL):
[wrt to this bug]
Actually, no, enums in the bug's subject and reduced test case are a
red herring. For example, this still fails for me on 4.1.1:

template< int C > int assertion_failed(int);
template< class > struct N
{
  static int const value = 1;
  static int const t = sizeof( assertion_failed<value>(0) );
};

int main()
{
  N<int> n;
  return n.t;
}


As a matter of fact, my current workaround for this issue involves
_introducing_ an auxiliary enum for the values that are used in
function templates' non-type arguments, e.g.:

template< int C > int assertion_failed(int);
template< class > struct N
{
  static int const value = 1;
  enum { gcc_workaround = value };
  static int const t = sizeof( assertion_failed<gcc_workaround>(0) ); // it compiles, yay!
};
Comment 13 Andrew Pinski 2006-11-12 16:43:12 UTC
(In reply to comment #12)
> From Aleksey Gurtovoy (co-author Boost.MPL):
> [wrt to this bug]
> Actually, no, enums in the bug's subject and reduced test case are a
> red herring. For example, this still fails for me on 4.1.1:

Not really because that passes for me with 4.1.2 and 4.2.0.

Comment 14 Mark Mitchell 2006-11-13 17:48:50 UTC
Subject: Bug 29518

Author: mmitchel
Date: Mon Nov 13 17:48:28 2006
New Revision: 118767

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118767
Log:
	PR c++/29518
	* pt.c (coerce_template_parms): Do not skip_evaluation while
	substituting template arguments.
	PR c++/29518
	* g++.dg/template/static28.C: New test.

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

Comment 15 Mark Mitchell 2006-11-13 17:49:53 UTC
Subject: Bug 29518

Author: mmitchel
Date: Mon Nov 13 17:49:43 2006
New Revision: 118768

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118768
Log:
	PR c++/29518
	* pt.c (coerce_template_parms): Do not skip_evaluation while
	substituting template arguments.
	PR c++/29518
	* g++.dg/template/static28.C: New test.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/testsuite/ChangeLog

Comment 16 Mark Mitchell 2006-11-13 17:52:09 UTC
Fixed in 4.3.0.
Comment 17 Mark Mitchell 2006-11-13 18:41:37 UTC
Subject: Bug 29518

Author: mmitchel
Date: Mon Nov 13 18:41:26 2006
New Revision: 118770

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118770
Log:
	PR c++/29518
	* pt.c (coerce_template_parms): Do not skip_evaluation while
	substituting template arguments.
	PR c++/29518
	* g++.dg/template/static28.C: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/template/static28.C
Modified:
    branches/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/gcc-4_1-branch/gcc/cp/pt.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 18 Mark Mitchell 2006-11-14 04:59:57 UTC
Subject: Bug 29518

Author: mmitchel
Date: Tue Nov 14 04:59:48 2006
New Revision: 118801

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118801
Log:
	PR c++/29518
	* pt.c (coerce_template_parms): Do not skip_evaluation while
	substituting template arguments.
	PR c++/29518
	* g++.dg/template/static28.C: New test.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/template/static28.C
Modified:
    branches/gcc-4_2-branch/gcc/cp/ChangeLog
    branches/gcc-4_2-branch/gcc/cp/pt.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 19 Mark Mitchell 2006-11-14 05:00:29 UTC
Fixed in 4.1.2, 4.2.0.
Comment 20 Andrew Pinski 2006-11-25 04:31:57 UTC
*** Bug 29701 has been marked as a duplicate of this bug. ***
Comment 21 Gabriel Dos Reis 2007-02-03 20:41:54 UTC
Fixed in GCC-4.1.2.