Bug 28370 - [4.2 Regression] undefined reference to template class static variable in an anonymous namespace
Summary: [4.2 Regression] undefined reference to template class static variable in an ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 critical
Target Milestone: 4.2.0
Assignee: Jakub Jelinek
URL:
Keywords: link-failure
Depends on:
Blocks:
 
Reported: 2006-07-13 09:41 UTC by Caolan McNamara
Modified: 2006-07-16 22:24 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-07-13 16:50:42


Attachments
testcase (10.84 KB, text/plain)
2006-07-13 09:42 UTC, Caolan McNamara
Details
original -6 nm output (350 bytes, text/plain)
2006-07-13 09:43 UTC, Caolan McNamara
Details
new nm -7 output (361 bytes, text/plain)
2006-07-13 09:43 UTC, Caolan McNamara
Details
diff against testme.ii which will make it work for me (290 bytes, patch)
2006-07-13 11:06 UTC, Caolan McNamara
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Caolan McNamara 2006-07-13 09:41:50 UTC
../unxlngi6.pro/slo/uuid.o: In function `create':
../../inc/rtl/instance.hxx:292: undefined reference to `(anonymous namespace)::rtl_Instance<(anonymous namespace)::Pool, rtl::Static<(anonymous namespace)::Pool, (anonymous namespace)::PoolHolder>::StaticInstance, osl::Guard<osl::Mutex>, osl::GetGlobalMutex, int, int>::m_pInstance'

So, attachment 1 is my testcase testme.ii

with "g++ -c -fPIC -o testme.o testme.ii"

compiled with "g++ (GCC) 4.1.1 20060629 (Red Hat 4.1.1-6)" I get the following
attachment 2 [details] output from nm

compiled with "g++ (GCC) 4.1.1 20060711 (Red Hat 4.1.1-7)" I get the following
attachment 3 [details] output from nm

the above missing symbol is U in -7 and V in -6
Comment 1 Caolan McNamara 2006-07-13 09:42:32 UTC
Created attachment 11877 [details]
testcase
Comment 2 Caolan McNamara 2006-07-13 09:43:03 UTC
Created attachment 11878 [details]
original -6 nm output
Comment 3 Caolan McNamara 2006-07-13 09:43:21 UTC
Created attachment 11879 [details]
new nm -7 output
Comment 4 Caolan McNamara 2006-07-13 11:06:14 UTC
Created attachment 11880 [details]
diff against testme.ii which will make it work for me

This attachment is a diff against the testme.ii and makes the instance static class member into a local static variable. Which is acceptable for my use case, and works around the problem for me
Comment 5 Andrew Pinski 2006-07-13 11:53:58 UTC
This is invalid code with no diagnostic required.
See the full explaination in PR 28360 which is exactly the same case.  In fact with a real testcase :).

*** This bug has been marked as a duplicate of 28360 ***
Comment 6 Andrew Pinski 2006-07-13 11:55:17 UTC
Basicially the code was violating ODR rules before because of the anonymous namespace.
Comment 7 Andrew Pinski 2006-07-13 12:19:33 UTC
Note I think you should have first checked FSF 4.1.2 which will have not this issue.  4.2.0 might have it, I might had been wrong at closing as a dup, that might be because I am tried and had a long day and it is 9:20 pm here already. 
Comment 8 Andrew Pinski 2006-07-13 14:08:03 UTC
Here is a full reduced self contained testcase:
namespace 
{
    template< typename Inst>
    struct rtl_Instance
{
    static int * m_pInstance;
};

template< typename Inst> int * rtl_Instance< Inst>::m_pInstance = 0;
}

int *foo(void)
{
    return rtl_Instance<int >::m_pInstance;
}
int main (void)
{
    return foo() == 0;
}
--------------------
It is only happens the namespace is anonymous which is why this is 4.2 only regression.  Jason also applied his patch to redhat's 4.1 branch.

Caolan, please next time test against a FSF release of the compiler before filing a bug because sometimes redhat backports patches to their branch.
Comment 9 Jakub Jelinek 2006-07-13 16:25:03 UTC
The reason why the var is not emitted seems to be that
note_vague_linkage_var is not called on it in finish_static_data_member_decl.
If I replace
  if (! processing_template_decl && TREE_PUBLIC (decl))
    note_vague_linkage_var (decl);
back with:
  if (! processing_template_decl)
    note_vague_linkage_var (decl);
it works just fine.
Comment 10 Jakub Jelinek 2006-07-13 16:50:42 UTC
Testing a patch.
Comment 11 Jakub Jelinek 2006-07-16 20:17:29 UTC
Subject: Bug 28370

Author: jakub
Date: Sun Jul 16 20:17:20 2006
New Revision: 115503

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115503
Log:
	PR c++/28370
	* decl2.c (note_vague_linkage_var): Removed.
	(finish_static_data_member_decl): Add decl to pending_statics vector
	directly.  Do it even for non-public decls.

	* g++.dg/template/anon3.C: New test.

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

Comment 12 Andrew Pinski 2006-07-16 22:24:21 UTC
Fixed.
Comment 13 Jakub Jelinek 2006-08-28 12:29:03 UTC
Subject: Bug 28370

Author: jakub
Date: Mon Aug 28 12:28:55 2006
New Revision: 116506

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116506
Log:
	PR c++/28370
	* decl2.c (note_vague_linkage_var): Removed.
	(finish_static_data_member_decl): Add decl to pending_statics vector
	directly.  Do it even for non-public decls.

	* g++.dg/template/anon3.C: New test.

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