Index: lib/Sema/SemaTemplateInstantiate.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiate.cpp +++ lib/Sema/SemaTemplateInstantiate.cpp @@ -1691,6 +1691,17 @@ ExprResult NewArg = SubstExpr(Arg, TemplateArgs); if (NewArg.isUsable()) NewParm->setDefaultArg(NewArg.get()); + } else if (OwningFunc->getLexicalDeclContext()->isFunctionOrMethod() && + !OwningFunc->isThisDeclarationADefinition()) { + // This is a function declaration within a function definition, as in: + // template void f() { + // void g(int x = T::v); + // } + Sema::ContextRAII SavedContext(*this, OwningFunc); + LocalInstantiationScope Local(*this); + ExprResult NewArg = SubstExpr(Arg, TemplateArgs); + if (NewArg.isUsable()) + NewParm->setDefaultArg(NewArg.get()); } else { // FIXME: if we non-lazily instantiated non-dependent default args for // non-dependent parameter types we could remove a bunch of duplicate Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3250,6 +3250,10 @@ if (CXXRecordDecl *Cls = dyn_cast(Tmpl->getDeclContext())) { if (Cls->isLocalClass()) RequireInstantiation = true; + } else if (Tmpl->getLexicalDeclContext()->isFunctionOrMethod() && + !Tmpl->isThisDeclarationADefinition()) { + // This is a non-defining declaration of a file scope function. + RequireInstantiation = true; } if (SemaRef.getLangOpts().CPlusPlus11 && EPI.ExceptionSpec.Type != EST_None && Index: test/SemaTemplate/default-arguments.cpp =================================================================== --- test/SemaTemplate/default-arguments.cpp +++ test/SemaTemplate/default-arguments.cpp @@ -159,3 +159,10 @@ int g() { X::f(0); } // expected-note {{in instantiation of template class 'DR1635::X' requested here}} } + +namespace NondefDecls { + template void f1() { + int g1(int defarg = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} + } + template void f1(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1' requested here}} +} Index: test/SemaTemplate/instantiate-exception-spec-cxx11.cpp =================================================================== --- test/SemaTemplate/instantiate-exception-spec-cxx11.cpp +++ test/SemaTemplate/instantiate-exception-spec-cxx11.cpp @@ -178,3 +178,11 @@ } } + +namespace NondefDecls { + template void f1() { + int g1(int) noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} + } + template void f1(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1' requested here}} +} +