Bug 13445

Summary: [3.3 Regression] Template argument replacement "dereferences" a typedef
Product: gcc Reporter: brad.king
Component: c++Assignee: Nathan Sidwell <nathan>
Status: RESOLVED FIXED    
Severity: critical CC: gcc-bugs, lerdsuwa
Priority: P2 Keywords: rejects-valid
Version: 3.3.2   
Target Milestone: 3.3.3   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed: 2003-12-19 15:39:19

Description brad.king 2003-12-19 14:54:31 UTC
Every version of gcc 3.3.2 I've tested has the bug.  I built a copy by
downloading sources from ftp.gnu.org to be sure no distribution-specific patches
were to blame:

../gcc-3.3.2/configure \
  --prefix=/home/kingb/Temp/gcc-3.3.2-install \
  --enable-languages=c,c++
make bootstrap
make install

"g++ --version" reports "g++ (GCC) 3.3.2"

The following code reproduces the problem.  It is expected to compile without error:

template <class T> class NestedBase {};
template <class T> class Base { protected: typedef int Protected; };
template <class T> struct Derived: public Base<T>
{
  typedef typename Base<T>::Protected Public;
  class Nested: public NestedBase<Public> {};
};
template class Derived<int>::Nested;

Compiling the source with "./g++ -c bug.cxx" produces this error:

bug.cxx: In instantiation of `Derived<int>::Nested':
bug.cxx:8:   instantiated from here
bug.cxx:2: error: `typedef int Base<int>::Protected' is protected
bug.cxx:8: error: within this context

We see that the "Public" typedef is "dereferenced" while instantiating the
template member class "Nested", which produces the access violation.

Compiling the same source with g++ 3.2.3 or any earlier GCC version produces no
error.
Comment 1 Andrew Pinski 2003-12-19 15:39:18 UTC
Broken: Search converges between 2002-07-14-trunk (#81) and 2002-07-21-trunk (#82).
Fixed on the mainline: Search converges between 2003-08-17-trunk (#375) and 2003-08-18-
trunk (#376).
Confirmed.
Comment 2 Wolfgang Bangerth 2003-12-19 16:41:09 UTC
Yup, this is bogus. It works on mainline, though. Kriang, you are the 
friendship expert, any chance to get this fixed on the branch? It is a 
regression with respect to 3.2.x. 
 
W. 
Comment 3 Wolfgang Bangerth 2003-12-19 16:41:41 UTC
Sorry, this has nothing to do with friendship, of course, and all 
with access control. 
W. 
Comment 4 brad.king 2003-12-19 19:45:53 UTC
I've discovered that applying this patch manually fixes the bug:

http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?r1=1.758&r2=1.759

I am unable to confirm whether it introduces any other bugs when included in the
3.3 branch.
Comment 5 Andrew Pinski 2003-12-19 19:51:55 UTC
It does look like that is the patch fixes it.
Nathan could you backport this patch for 3.3.3:
cp:
        * pt.c (instantiate_class_template): Push to class's scope before
        tsubsting base.
testsuite:
        * g++.dg/template/scope2.C: New test.
        * g++.dg/template/error2.C: Correct dg-error
Comment 6 GCC Commits 2003-12-30 09:47:37 UTC
Subject: Bug 13445

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	nathan@gcc.gnu.org	2003-12-30 09:47:32

Modified files:
	gcc/cp         : ChangeLog pt.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/template: scope2.C 

Log message:
	cp:
	PR c++/13445
	* pt.c (instantiate_class_template): Push to class's scope before
	tsubsting base.
	testsuite:
	PR c++/13445
	* g++.dg/template/scope2.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.231&r2=1.3076.2.232
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.635.2.39&r2=1.635.2.40
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.347&r2=1.2261.2.348
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/scope2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.24.1

Comment 7 Nathan Sidwell 2003-12-30 09:49:07 UTC
2003-12-30  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/13445
	* pt.c (instantiate_class_template): Push to class's scope before
	tsubsting base.