Bug 33094 - [4.2 Regression] ICE on valid C++ virtual template static member in anonymous namespace
Summary: [4.2 Regression] ICE on valid C++ virtual template static member in anonymous...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P1 normal
Target Milestone: 4.2.3
Assignee: Jason Merrill
URL:
Keywords: ice-on-valid-code
: 33322 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-08-17 03:26 UTC by Ian Lance Taylor
Modified: 2007-10-09 21:11 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0
Known to fail:
Last reconfirmed: 2007-09-23 18:22:01


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lance Taylor 2007-08-17 03:26:51 UTC
This C++ code gets an ICE with gcc 4.2 and with mainline.  I believe this is valid C++ code.

namespace
{

template <typename T>
class A
{
  virtual T f1() { return c; }
  static const T c = 0;
};

A<int> v;

}

foo.cc: In instantiation of ‘const int <unnamed>::A<int>::c’:
foo.cc:7:   instantiated from ‘T<unnamed>::A<T>::f1() [with T = int]’
foo.cc:11:   instantiated from here
foo.cc:8: internal compiler error: in make_rtl_for_nonlocal_decl, at cp/decl.c:4967
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Comment 1 Andrew Pinski 2007-08-17 09:29:09 UTC
      /* An in-class declaration of a static data member should be
         external; it is only a declaration, and not a definition.  */
      if (init == NULL_TREE)
        gcc_assert (DECL_EXTERNAL (decl));


Confirmed.
Comment 2 Ian Lance Taylor 2007-08-17 14:31:26 UTC
This patch fixes the problem and passes the g++ testsuite.

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 127491)
+++ cp/decl.c	(working copy)
@@ -4963,7 +4963,7 @@ make_rtl_for_nonlocal_decl (tree decl, t
       gcc_assert (TREE_STATIC (decl));
       /* An in-class declaration of a static data member should be
 	 external; it is only a declaration, and not a definition.  */
-      if (init == NULL_TREE)
+      if (init == NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
 	gcc_assert (DECL_EXTERNAL (decl));
     }
 
Comment 3 Mark Mitchell 2007-09-05 01:00:30 UTC
Ian, I know that we talked about this on the mailing list at one point.  Did this get resolved?  Does changing the assert to check DECL_EXTERNAL_LINKAGE_P instead of DECL_EXTERNAL help?
Comment 4 Ian Lance Taylor 2007-09-05 06:03:29 UTC
I haven't looked further at this since this message:

http://gcc.gnu.org/ml/gcc-patches/2007-08/msg01166.html

Testing DECL_EXTERNAL_LINKAGE_P does not make any difference: the compiler still crashes.  The decl in question is

 <var_decl 0xb7d3b0b8 c
    type <integer_type 0xb7d378dc int readonly type_6 SI
        size <integer_cst 0xb7c55620 constant invariant 32>
        unit size <integer_cst 0xb7c5540c constant invariant 4>
        align 32 symtab 0 alias set -1 canonical type 0xb7d378dc precision 32 min <integer_cst 0xb7c555cc -2147483648> max <integer_cst 0xb7c555e8 2147483647>>
    readonly used private static tree_1 tree_2 tree_3 nonlocal decl_3 decl_5 decl_6 SI file /home/iant/foo1.cc line 8 size <integer_cst 0xb7c55620 32> unit size <integer_cst 0xb7c5540c 4>
    align 32 context <record_type 0xb7d37ca8 A> initial <integer_cst 0xb7c55bd0 0>
    template-info 0xb7d3c444 chain <type_decl 0xb7d37d80 A>>
Comment 5 Andrew Pinski 2007-09-07 00:28:55 UTC
*** Bug 33322 has been marked as a duplicate of this bug. ***
Comment 6 Jason Merrill 2007-09-30 02:41:55 UTC
Subject: Bug 33094

Author: jason
Date: Sun Sep 30 02:41:39 2007
New Revision: 128890

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128890
Log:
        PR c++/33094
        * decl.c (make_rtl_for_nonlocal_decl): It's ok for a member
        constant to not have DECL_EXTERNAL if it's file-local.

Added:
    trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/decl2.c

Comment 7 Mark Mitchell 2007-10-09 19:20:42 UTC
Change target milestone to 4.2.3, as 4.2.2 has been released.
Comment 8 Jason Merrill 2007-10-09 20:52:40 UTC
Subject: Bug 33094

Author: jason
Date: Tue Oct  9 20:52:24 2007
New Revision: 129180

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129180
Log:
        PR c++/32470
        * name-lookup.c (push_namespace_with_attrs): Fold back into...
        (push_namespace): Here.
        (handle_namespace_attrs): New fn for the attr code.
        (leave_scope): Don't pop_visibility.
        * name-lookup.h (struct cp_binding_level): Remove has_visibility.
        * parser.c (cp_parser_namespace_definition): Call
        handle_namespace_attrs and pop_visibility as appropriate.

        PR c++/33094
        * decl.c (make_rtl_for_nonlocal_decl): It's ok for a member
        constant to not have DECL_EXTERNAL if it's file-local.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/ext/visibility/anon6.C
      - copied unchanged from r128890, trunk/gcc/testsuite/g++.dg/ext/visibility/anon6.C
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/ext/visibility/namespace2.C
      - copied unchanged from r129003, trunk/gcc/testsuite/g++.dg/ext/visibility/namespace2.C
Modified:
    branches/gcc-4_2-branch/gcc/cp/ChangeLog
    branches/gcc-4_2-branch/gcc/cp/decl.c
    branches/gcc-4_2-branch/gcc/cp/name-lookup.c
    branches/gcc-4_2-branch/gcc/cp/name-lookup.h
    branches/gcc-4_2-branch/gcc/cp/parser.c

Comment 9 Jason Merrill 2007-10-09 21:11:00 UTC
Fixed for 4.2.3.
Comment 10 Jason Merrill 2007-10-22 18:12:51 UTC
Subject: Bug 33094

Author: jason
Date: Mon Oct 22 18:12:36 2007
New Revision: 129554

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129554
Log:
        PR c++/32470
        * name-lookup.c (push_namespace_with_attrs): Fold back into...
        (push_namespace): Here.
        (handle_namespace_attrs): New fn for the attr code.
        (leave_scope): Don't pop_visibility.
        * name-lookup.h (struct cp_binding_level): Remove has_visibility.
        * parser.c (cp_parser_namespace_definition): Call
        handle_namespace_attrs and pop_visibility as appropriate.

        PR c++/33094
        * decl.c (make_rtl_for_nonlocal_decl): It's ok for a member
        constant to not have DECL_EXTERNAL if it's file-local.

        * decl2.c (get_guard): Copy visibility from the guarded variable.

        PR c++/29365
        * pt.c (outermost_tinst_level): New function.
        * lex.c (in_main_input_context): New function.
        * decl2.c (constrain_class_visibility): Use it to avoid warning
        about uses of the anonymous namespace in the main input file.

Modified:
    branches/redhat/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/redhat/gcc-4_1-branch/gcc/cp/cp-tree.h
    branches/redhat/gcc-4_1-branch/gcc/cp/decl.c
    branches/redhat/gcc-4_1-branch/gcc/cp/decl2.c
    branches/redhat/gcc-4_1-branch/gcc/cp/lex.c
    branches/redhat/gcc-4_1-branch/gcc/cp/name-lookup.c
    branches/redhat/gcc-4_1-branch/gcc/cp/name-lookup.h
    branches/redhat/gcc-4_1-branch/gcc/cp/parser.c
    branches/redhat/gcc-4_1-branch/gcc/cp/pt.c