This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR c++/38089: g++ crash on invalid code


Hi all.

The following invalid snippet triggers an ICE since 4.1

=== cut ===
struct basic_string
{
          basic_string(const int __s);
};
namespace MyNS {
          class MyClass {
            template <typename T>
            T test() { }
          };
}
template <>
basic_string MyNS::MyClass::test(){ return 1;}
=== cut ===

The problem, I think, lies in the way the DECL_CONTEXT for the
specialization is set when we detect that it is not in the correct
namespace: we try to give it the context of the template it's
specializing, but we only consider namespaces. As a consequence, we end
up with a class member with a namespace context, which causes the ICE later.

The attached patch fixes this by setting the specialization's
DECL_CONTEXT simply to the template's DECL_CONTEXT. The code has been
like this since the beginning (i.e. when it was added, in 3.4.2, to fix
PR 16224), but it looks wrong (in case there's some tricky case I don't
see and it's correct, returning error_mark_node also fixes the PR...).

I've successfully regtested this on x86_64-apple-darwin-9. Is it OK for
the trunk?

Best regards,
Simon

:ADDPATCH c++:








2009-06-01  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/38089
	* pt.c (register_specialization): Properly setup DECL_CONTEXT for
	specializations in an invalid namespace.








Index: gcc/cp/pt.c
===================================================================
--- gcc/cp/pt.c	(revision 148050)
+++ gcc/cp/pt.c	(working copy)
@@ -1316,7 +1316,7 @@ register_specialization (tree spec, tree
      template it is specializing.  */
   if (DECL_TEMPLATE_SPECIALIZATION (spec)
       && !check_specialization_namespace (tmpl))
-    DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
+    DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
 
   if (!optimize_specialization_lookup_p (tmpl))
     DECL_TEMPLATE_SPECIALIZATIONS (tmpl)








2009-06-01  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/38089
	* g++.dg/template/spec36.C: New test.








/* PR c++/38089 */
/* { dg-do "compile" } */

struct basic_string
{
  basic_string(const int __s);
};
namespace MyNS {
  class MyClass {
    template <typename T>
    T test() { } /* { dg-error "from definition" } */
  };
}
template <>
basic_string MyNS::MyClass::test() /* { dg-error "specialization of" } */
{ return 1; }









Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]