Bug 28016

Summary: [4.1 Regression] emitting template constant
Product: gcc Reporter: bredelin
Component: c++Assignee: Mark Mitchell <mark>
Status: RESOLVED FIXED    
Severity: normal CC: bangerth, debian-gcc, fang, gcc-bugs, lidaobing, mark, matz, peter.soetens, pinskia, pintaric, rguenth, tausq, tbm
Priority: P1 Keywords: link-failure, wrong-code
Version: 4.2.0   
Target Milestone: 4.1.2   
Host: Target:
Build: Known to work: 4.1.0
Known to fail: 4.1.2 4.2.0 Last reconfirmed: 2006-06-13 18:49:08

Description bredelin 2006-06-13 18:07:04 UTC
I have some software that uses the BOOST matrix library UBLAS (1.33.1).  This software compiles and links with GCC 4.1.1 (Debian Linux system - GNU ld) but gives linking errors with GCC 4.2:

substitution.o:(.data+0x0): multiple definition of `_ZN5boost7numeric5ublas21scalar_divides_assignIT_T0_E8computedE'
alignment.o:(.data+0x0): first defined here

Here is from the definition of the static const member of a template class in boost/numeric/ublas/functional.hpp:

template<class T1, class T2>
struct scalar_divides_assign:
public scalar_binary_assign_functor<T1, T2> {
      ... stuff ...
};

template<class T1, class T2>
const bool scalar_divides_assign<T1,T2>::computed = true;

GCC 4.2, but not 4.1 actually emits this constant in the global data section.  With 4.2:

$ nm alignment.o | grep divides
0000000000000180 t _GLOBAL__I__ZN5boost7numeric5ublas21scalar_divides_assignIT_T0_E8computedE
0000000000000000 D _ZN5boost7numeric5ublas21scalar_divides_assignIT_T0_E8computedE

With 4.1 neither of these two symbols (or is it one symbol, mentioned twice?) is emitted.
Comment 1 bredelin 2006-06-13 18:30:54 UTC
Here is some source code that exhibits the problem:
---------------- begin a.C -----------------
template<class T1, class T2>
struct scalar_divides_assign {
  static const bool computed ;
};

template<class T1, class T2>
const bool scalar_divides_assign<T1,T2>::computed = true;
----------------- end ------------------

To see the problem do 
$ g++-4.2 -c a.C
$ nm a.o | grep computed
0000000000000000 D _ZN21scalar_divides_assignIT_T0_E8computedE

This symbol shouldn't be emitted, or at least not in this way.
Comment 2 Andrew Pinski 2006-06-13 18:49:08 UTC
Confirmed.
(In reply to comment #1)

> template<class T1, class T2>
> const bool scalar_divides_assign<T1,T2>::computed = true;

For this case above, nothing should be emitted.
Comment 3 Andrew Pinski 2006-06-13 18:52:07 UTC
It is only static const variables which are miss handled.
Comment 4 happyarch 2006-06-14 07:03:44 UTC
boostjam get segfault to build boost with gcc-4.2

 > pwd
/home/keti/download/boost_1_33_1
 >
 > make
./tools/build/jam_src/bin.linuxx86/bjam -sPYTHON_ROOT=/usr -sPYTHON_VERSION=2.4 -sTOOLS=gcc
/bin/sh: line 1: 27533 
Comment 5 Martin Michlmayr 2006-06-16 14:46:37 UTC
This also fails with 4.1 from SVN.  The problem got introduced between 20060530
and 20060613.
Comment 6 Martin Michlmayr 2006-06-16 14:47:48 UTC
(In reply to comment #4)
> boostjam get segfault to build boost with gcc-4.2

I can confirm this, but this is a completely different issue.  Unfortunately, I don't know if this is a bug in boost or gcc, but I suspect the former.
Comment 7 Martin Michlmayr 2006-06-16 15:11:03 UTC
This was caused by this commit:

2006-06-04  Mark Mitchell  <mark@codesourcery.com>

        PR c++/27819
        * decl.c (cp_finish_decl): Process initializers for static data
        members with non-dependent initializers, even in templates.
Comment 8 Andrew Pinski 2006-06-16 19:01:03 UTC
*** Bug 28065 has been marked as a duplicate of this bug. ***
Comment 9 Mark Mitchell 2006-06-17 01:11:44 UTC
Subject: Bug 28016

Author: mmitchel
Date: Sat Jun 17 01:11:34 2006
New Revision: 114739

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114739
Log:
	PR c++/28016
	* decl.c (cp_finsh_decl): Do not emit uninstantiated static data
	members.
	PR c++/28016
	* g++.dg/template/static26.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/template/static26.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Mark Mitchell 2006-06-17 01:14:16 UTC
Fixed in 4.2.0.
Comment 11 Andrew Pinski 2006-07-10 08:53:21 UTC
*** Bug 28327 has been marked as a duplicate of this bug. ***
Comment 12 Michael Matz 2006-07-10 08:57:12 UTC
Mark, please apply to 4.1 branch also.
Comment 13 Mark Mitchell 2006-07-12 07:22:59 UTC
Subject: Bug 28016

Author: mmitchel
Date: Wed Jul 12 07:22:47 2006
New Revision: 115365

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115365
Log:
	PR c++/28016
	* decl.c (cp_finsh_decl): Do not emit uninstantiated static data
	members.
	PR c++/28016
	* g++.dg/template/static26.C: New test.

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

Comment 14 Mark Mitchell 2006-07-12 07:23:38 UTC
Fixed in 4.1.2.
Comment 15 Martin Michlmayr 2006-08-04 15:10:23 UTC
*** Bug 28599 has been marked as a duplicate of this bug. ***
Comment 16 Martin Michlmayr 2006-08-06 13:41:29 UTC
*** Bug 28620 has been marked as a duplicate of this bug. ***