Bug 28058 - [4.1/4.2 regression] ICE in inline_forbidden_p
Summary: [4.1/4.2 regression] ICE in inline_forbidden_p
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.1.2
Assignee: Mark Mitchell
URL:
Keywords: ice-on-invalid-code
: 28159 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-06-16 13:06 UTC by Jakub Jelinek
Modified: 2006-08-27 18:00 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-06-16 22:51:36


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2006-06-16 13:06:41 UTC
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).
Comment 1 Jakub Jelinek 2006-06-16 13:47:06 UTC
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.
Comment 2 Wolfgang Bangerth 2006-06-16 22:51:35 UTC
Confirmed.
Comment 3 Wolfgang Bangerth 2006-06-16 22:55:20 UTC
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.
Comment 4 Andrew Pinski 2006-06-24 23:53:44 UTC
*** Bug 28159 has been marked as a duplicate of this bug. ***
Comment 5 Volker Reichelt 2006-07-12 16:39:30 UTC
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.)
Comment 6 Jan Hubicka 2006-07-21 20:36:01 UTC
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
Comment 7 Janis Johnson 2006-08-14 16:16:51 UTC
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)
Comment 8 Mark Mitchell 2006-08-26 16:53:44 UTC
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.)
Comment 9 Mark Mitchell 2006-08-27 17:45:32 UTC
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

Comment 10 Mark Mitchell 2006-08-27 18:00:05 UTC
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

Comment 11 Mark Mitchell 2006-08-27 18:00:53 UTC
Fixed in 4.1.2.