From patchwork Fri Oct 1 21:18:14 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: c++/42018 try to reject specialization re-declared in wrong namespace From: Jonathan Wakely X-Patchwork-Id: 66520 Message-Id: To: gcc-patches Date: Fri, 1 Oct 2010 22:18:14 +0100 This patch is my attempt to fix PR c++/42018, but it's wrong, because it also rejects some valid specializations, causing: FAIL: g++.old-deja/g++.ns/template12.C (test for excess errors) Excess errors: /home/redi/src/gcc/gcc-4.x/gcc/testsuite/g++.old-deja/g++.ns/template12.C:18:47: error: specialization of 'template const T bar::foo(const T&)' in different namespace [-fpermissive] /home/redi/src/gcc/gcc-4.x/gcc/testsuite/g++.old-deja/g++.ns/template12.C:13:20: error: from definition of 'template const T bar::foo(const T&)' [-fpermissive] FAIL: g++.old-deja/g++.pt/memtemp96.C (test for excess errors) Excess errors: /home/redi/src/gcc/gcc-4.x/gcc/testsuite/g++.old-deja/g++.pt/memtemp96.C:11:17: error: specialization of 'template template int A::f(U)' must appear at namespace scope Can anyone point me in the right direction so that the new test in the patch fails, but those don't? Thanks, Jonathan Index: gcc/cp/pt.c =================================================================== --- gcc/cp/pt.c (revision 164893) +++ gcc/cp/pt.c (working copy) @@ -1379,6 +1379,11 @@ register_specialization (tree spec, tree } else if (DECL_TEMPLATE_SPECIALIZATION (fn)) { + /* A specialization must be declared in the same namespace as the + template it is specializing. */ + if (!check_specialization_namespace (tmpl)) + return error_mark_node; + if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) /* Dup decl failed, but this is a new definition. Set the line number so any errors match this new Index: gcc/testsuite/g++.dg/template/pr42018.C =================================================================== --- gcc/testsuite/g++.dg/template/pr42018.C (revision 0) +++ gcc/testsuite/g++.dg/template/pr42018.C (revision 0) @@ -0,0 +1,14 @@ +// PR c++/42018 +// { dg-do compile } + +template + void foo(void); // { dg-error "from definition of" } + +template<> + void foo(); + +namespace x { + template<> + void foo() { return; } // { dg-error "in different namespace" } +} +