struct A {}; template <class T> struct B : public T { B (); }; B<A> * foo () { return new B<A>(); } template <class T> B<T>::B () { } template <> B<A>::B () { } ICEs at -O3 i inline_forbidden_p: 1448 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl)) as fndecl (B ctor) doesn't have cfg set (DECL_STRUCT_FUNCTION (fndecl)->cfg == NULL).
The problem seems to be that cp_cannot_inline_tree_fn changes *fnp to whatever instantiate_decl returned and that function has not been lowered yet. Not sure if it is at all possible to cgraph_lower_function from within inlinable_function_p (i.e. if cgraph_analyze_function is reentrant) and if yes, if that's supposed to be cp_cannot_inline_tree_fn's responsibility. In any case, the testcase works if foo is moved to the end of the file, at which point the template is already lowered.
Confirmed.
The code is invalid, however: explicit specializations must be declared before they are first used. In the snippet, B<A>::B is used in foo() before the specialization is declared. The ICE consequently also goes away if a declaration (not definition) of the specialization precedes foo(). That all doesn't make it any more forgivable that we ICE on this code, however. W.
*** Bug 28159 has been marked as a duplicate of this bug. ***
Here's a testcase that crashes even at -O0 since GCC 4.1.0: =========================== template<int> struct A { A() {} }; A<0> a; template<> A<0>::A() {} =========================== As Wolfgang already mentioned: explicit specializations must be declared before they are first used. However, no diagnostic is required. (Which indicates that such situations are hard to detect reliably.)
cgraph_analyze_function is reentrant for non-unit-at-a-time support. However with unit-at-a-time, the analysis happens only after unit has been finalized and at that time frontend is not supposed to come up with new functions anymore. It would be a lot better to make C++ frontend to handle this case early (probably diagnose the invalid code?), but I don't quite understand this area of compiler, so if someone C++ aware would tell me it is dificult, we can do it as workaround. Honza
Regression hunts on powerpc-linux showed that the original testcase with -O3 and the testcase in comment #5 with -O0 both start getting segfaults with the following patch: http://gcc.gnu.org/viewcvs?view=rev&rev=99840 r99840 | hubicka | 2005-05-17 16:56:32 +0000 (Tue, 17 May 2005)
We do have code to notice use-before-specialization errors. For example: template <typename T> void f(); void g() { f<int>(); } template <> void f<int>() {} results in an error. However, the code to initialize static variables is not generated until end-of-file, so we do not realize that we are in this situation. (See expand_static_init for the point at which we defer initialization.)
Subject: Bug 28058 Author: mmitchel Date: Sun Aug 27 17:45:25 2006 New Revision: 116491 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116491 Log: PR c++/28058 * pt.c (register_specialization): Return error_mark_node for specialization-after-instantiation. * decl2.c (mark_used): Mark the main function used when one of its clones is used. PR c++/28058 * g++.dg/template/spec31.C: New test. Added: trunk/gcc/testsuite/g++.dg/template/spec31.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/decl2.c trunk/gcc/cp/pt.c trunk/gcc/testsuite/ChangeLog
Subject: Bug 28058 Author: mmitchel Date: Sun Aug 27 17:59:55 2006 New Revision: 116492 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116492 Log: PR c++/28058 * pt.c (register_specialization): Return error_mark_node for specialization-after-instantiation. * decl2.c (mark_used): Mark the main function used when one of its clones is used. PR c++/28058 * g++.dg/template/spec31.C: New test. * g++.old-deja/g++.pt/spec9.C: Adjust error markers. Added: branches/gcc-4_1-branch/gcc/testsuite/g++.dg/template/spec31.C Modified: branches/gcc-4_1-branch/gcc/cp/ChangeLog branches/gcc-4_1-branch/gcc/cp/decl2.c branches/gcc-4_1-branch/gcc/cp/pt.c branches/gcc-4_1-branch/gcc/testsuite/ChangeLog branches/gcc-4_1-branch/gcc/testsuite/g++.old-deja/g++.pt/spec9.C
Fixed in 4.1.2.