Index: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td @@ -4106,6 +4106,13 @@ def err_explicit_specialization_inconsistent_storage_class : Error< "explicit specialization has extraneous, inconsistent storage class " "'%select{none|extern|static|__private_extern__|auto|register}0'">; +def err_dependent_function_template_spec_no_match : Error< + "no candidate function template was found for dependent" + " friend function template specialization">; +def note_dependent_function_template_spec_discard_reason : Note< + "candidate ignored: %select{not a function template" + "|not a member of the enclosing namespace;" + " did you mean to explicitly qualify the specialization?}0">; // C++ class template specializations and out-of-line definitions def err_template_spec_needs_header : Error< Index: cfe/trunk/lib/Sema/SemaTemplate.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaTemplate.cpp +++ cfe/trunk/lib/Sema/SemaTemplate.cpp @@ -8003,17 +8003,34 @@ // the correct context. DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); LookupResult::Filter F = Previous.makeFilter(); + enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; + SmallVector, 8> DiscardedCandidates; while (F.hasNext()) { NamedDecl *D = F.next()->getUnderlyingDecl(); - if (!isa(D) || - !FDLookupContext->InEnclosingNamespaceSetOf( - D->getDeclContext()->getRedeclContext())) + if (!isa(D)) { F.erase(); + DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); + continue; + } + + if (!FDLookupContext->InEnclosingNamespaceSetOf( + D->getDeclContext()->getRedeclContext())) { + F.erase(); + DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); + continue; + } } F.done(); - // Should this be diagnosed here? - if (Previous.empty()) return true; + if (Previous.empty()) { + Diag(FD->getLocation(), + diag::err_dependent_function_template_spec_no_match); + for (auto &P : DiscardedCandidates) + Diag(P.second->getLocation(), + diag::note_dependent_function_template_spec_discard_reason) + << P.first; + return true; + } FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), ExplicitTemplateArgs); Index: cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp =================================================================== --- cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp +++ cfe/trunk/test/CXX/temp/temp.decls/temp.friend/p1.cpp @@ -359,3 +359,31 @@ template void f2(X *); template void f2(X *); // expected-note{{in instantiation of function template specialization 'PR10913::f2' requested here}} } + +namespace test16 { +template struct foo {}; // expected-note{{candidate ignored: not a function template}} +template class A { + friend void foo(); // expected-error{{no candidate function template was found for dependent friend function template specialization}} +}; +} + +namespace test17 { +namespace ns { +template void foo() {} // expected-note{{candidate ignored: not a member of the enclosing namespace; did you mean to explicitly qualify the specialization?}} +} +using ns::foo; +template struct A { + friend void foo() {} // expected-error{{no candidate function template was found for dependent friend function template specialization}} +}; +} + +namespace test18 { +namespace ns1 { template struct foo {}; } // expected-note{{candidate ignored: not a function template}} +namespace ns2 { void foo() {} } // expected-note{{candidate ignored: not a function template}} +using ns1::foo; +using ns2::foo; + +template class A { + friend void foo() {} // expected-error{{no candidate function template was found for dependent friend function template specialization}} +}; +}